实例化时未调用PHP构造函数 [英] PHP Constructor Not Called Upon Instantiation
问题描述
启动类时,似乎没有调用我的PHP类构造函数.这是我的构造函数的样子:
My PHP class constructor appears to not be getting called when the class is initiated. This is what my constructor looks like:
public function __contruct()
{
$GLOBALS['page_content'] .= "<p>Constructor entered.</p>\r\n";
try
{
$this->ConstructorBase();
}
catch ( Exception $e )
{
throw new Exception(
"Error in ".__FILE__."(".__LINE__."): Constructor failed.",
CLoginError::ERROR_CANNOT_INSTANTIATE, $e );
}
}
稍后在全局范围内的同一文件中,我尝试实例化该类:
Later in the same file, in the global scope, I attempt to instantiate the class:
$Login = new CLogin();
但是,当我检查$GLOBALS['page_content']
时,在实例化该类之后,该类为空,就好像从未调用过构造函数一样.奇怪的是我可以调用公共成员函数.如果您想查看它,请在此处发布完整的源代码:
However, when I inspect $GLOBALS['page_content']
, after instantiating the class, it is empty, as if the constructor was never called. What is odd is that I can call public member functions. If you want to see it, the full source is posted here:
推荐答案
您已将函数__contruct()
命名为__construct()
.这是一个非常常见的错误,您可能应该睡一觉.
You named your function __contruct()
where it should be __construct()
. This is a very common error, you should probably get some sleep.
这篇关于实例化时未调用PHP构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!