【发布时间】:2011-07-29 11:11:50
【问题描述】:
可能的重复:
Reference - What does this symbol mean in PHP?
In, PHP, what is the “->” operator called and how do you say it when reading code out loud?
这是一个非常新手的问题,所以提前道歉,但我已经看到 -> 在示例代码中多次使用,但我似乎无法在在线教程中找到任何解释它的作用。 (主要是因为 Google 忽略了它作为搜索词 - 哦!)
这是一个让我感到困惑的例子:
<?php
class customException extends Exception
{
public function errorMessage()
{
//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
return $errorMsg;
}
}
$email = "someone@example.com";
try
{
//check if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
//throw exception if email is not valid
throw new customException($email);
}
//check for "example" in mail address
if(strpos($email, "example") !== FALSE)
{
throw new Exception("$email is an example e-mail");
}
}
catch (customException $e)
{
echo $e->errorMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
echo $e->errorMessage(); 等行中发生了什么?它看起来像是将变量 $e 传递给函数 errorMessage(),但如果是这样,为什么不以更传统的方式进行呢?
感谢您的帮助。
【问题讨论】:
-
这不是这两件事的重复,但还是谢谢。
-
查看here,第二个可能的重复项不正确。