您可以使用以下代码来查找登录用户的电子邮件:
$customer = Mage::getSingleton('customer/session')->getCustomer();
$mail = $customer->getEmail();如何找出定义getEmail()的位置?
我在app\code\core\Mage\Customer\Model\Resource\Customer.php中搜索,但没有名为getEmail()的函数。
我只找到了这个:

所以我用NetBeans回溯了一下,在lib\Varien\Object.php中找到了Varien_Object的定义。
但是在里面也没有getEmail()函数。
我在整个项目中搜索了字符串public function getEmail(),
这就是结果:
$ grep -iR "public function getEmail()"
app/code/core/Mage/Catalog/Block/Product/Send.php: public function getEmail()
app/code/core/Mage/Newsletter/Model/Subscriber.php: public function getEmail()
app/code/core/Mage/Sendfriend/Block/Send.php: public function getEmail()
app/code/core/_193_Mage/Catalog/Block/Product/Send.php: public function getEmail()
app/code/core/_193_Mage/Newsletter/Model/Subscriber.php: public function getEmail()
app/code/core/_193_Mage/Sendfriend/Block/Send.php: public function getEmail()
app/code/_core/Mage/Catalog/Block/Product/Send.php: public function getEmail()
app/code/_core/Mage/Newsletter/Model/Subscriber.php: public function getEmail()
app/code/_core/Mage/Sendfriend/Block/Send.php: public function getEmail()
lib/Payone/Api/Request/Parameter/Authorization/PersonalData.php: public function getEmail()
lib/Payone/Api/Request/Parameter/CreateAccess/PersonalData.php: public function getEmail()
lib/Payone/Api/Request/Parameter/ManageMandate/PersonalData.php: public function getEmail()
lib/Payone/Api/Request/Parameter/Vauthorization/PersonalData.php: public function getEmail()
lib/Zend/Gdata/App/Extension/Person.php: public function getEmail()
lib/Zend/Gdata/Extension/Who.php: public function getEmail()
lib/Zend/Service/ReCaptcha/MailHide.php: public function getEmail()
lib/Zend/View/Helper/Gravatar.php: public function getEmail()发布于 2018-07-29 19:43:01
你找不到getEmail()定义,因为它不存在。
正如您在共享的代码片段中所看到的,$customer是Varien_Object的一个实例。该类是在lib\Varien\Object.php中定义的。
如果你偷看那里,你会发现这个方法也没有定义……但这是因为Magento利用了PHP的magic methods。
在此$customer实例上调用不存在的方法时,将改为执行__call()。
这是Varien_Object::_call()的方法定义
/**
* Set/Get attribute wrapper
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
switch (substr($method, 0, 3)) {
case 'get' :
$key = $this->_underscore(substr($method,3));
$data = $this->getData($key, isset($args[0]) ? $args[0] : null);
return $data;
case 'set' :
$key = $this->_underscore(substr($method,3));
$result = $this->setData($key, isset($args[0]) ? $args[0] : null);
return $result;
case 'uns' :
$key = $this->_underscore(substr($method,3));
$result = $this->unsetData($key);
return $result;
case 'has' :
$key = $this->_underscore(substr($method,3));
return isset($this->_data[$key]);
}
throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
}从这里开始,逻辑非常简单。由于$method将为getEmail,因此将执行开关的第一个分支,并从方法名中获取$key (跳过前三个字符,因为它们必须是"get“、"set”、"uns“或"has"),在本例中将变为"email”。
https://stackoverflow.com/questions/51495187
复制相似问题