PHP Constructor Not Called Upon Instantiation [clo

2019-06-20 06:16发布

问题:

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();

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:

http://pastebin.com/D95YnUmS

回答1:

You named your function __contruct() where it should be __construct(). This is a very common error, you should probably get some sleep.