我在此上找不到任何好的答案,所以我要问它。我很难相信以前没有人问过这个问题。好的,我注意到以下我不明白的PHP代码:
$path = new Path('/a/b/c/d');
echo $path->cd('../x')->currentPath;
如果我没记错的话,$path
这里是Path类。并且cd
是功能。但是我也看到了这currentPath
一点。
当我尝试它时,我得到一个通知:
Trying to get property of non-object in line ##
所以我想弄清楚这一点。如果我是对的,则称为全局变量currentPath
,然后将在__construct
?中定义它。
只需返回$ this。
class Path
{
public $currentPath;
function __construct($path)
{
$this->currentPath = $path;
}
public function cd($newPath)
{
$this->currentPath = $newPath;
return $this;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句