可能的重复:
参考 - PHP 中这个符号是什么意思?
在 PHP 中,“->” 运算符被称为什么,如何在读取代码时说出它的名称?
这是一个非常新手级别的问题,所以提前道歉,但我在示例代码中看到过 ->
的使用,但我似乎找不到任何在线教程对它的解释(主要是因为 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()
,但如果是这样,为什么不采用更传统的方式做呢?感谢您的任何帮助。