PHP Constructor Not Called Upon Instantiation
You named your function __contruct()
where it should be __construct()
. This is a very common error, you should probably get some sleep.
Jim Fell
Technical expertise resides in developing firmware and PC-interface software for embedded systems using C/C++/C# programming languages, integrated development environments, and low-level debugging techniques. I enjoy developing embedded and PC drivers for wired communications. Previous work focused on the development of firmware for USB, CAN, SPI, RS-232, RS-485, ZigBee, and analog drivers, as well as implementing low-power embedded solutions across a broad range of embedded and desktop platforms.
Updated on June 15, 2022Comments
-
Jim Fell almost 2 years
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:-
meze over 12 yearsAlso GLOBALS and exceptions in constructors are bad practice
-
Mathieu Dumoulin over 12 yearsExceptions are not bad practices at all in constructors, it is actually recommended in any cases to trigger_error and is the only valid mechanism to catch errors that occur while creating objects. I do agree though on the GLOBALS, do not use GLOBALS anywhere... bad bad bad practice.
-
Madara's Ghost over 12 yearsConstructors should generally only have variable declarations in them. The rest is up to the methods.
-
Jim Fell over 12 yearsI was using GLOBALS just for debugging purposes because I needed to know if it was entering the constructor without breaking anything else. Thanks for the help everyone!
-
igorw over 12 yearsSide note: read up on "SQL injection" and "Prepared statements".
-
-
Jim Fell over 12 yearsTo think that this "typo" has kept me stuck for the last 3 weeks. ^_^;; I think I'm going to call it the typo from hell. Live and learn. Thanks for your help. I'm glad to be moving forward again, and at least I learned a lot about debugging PHP in the process.
-
Madara's Ghost over 12 yearsAye, if a function is not being called when it's supposed to (either on object instantiation or on a regular function call), assuming no errors are logged, it's very likely you've just mistyped the function name (either on the call or on the function declaration). Good luck next time :D
-
teynon over 11 yearsJust spent almost 5 minutes staring at those two above and wondering... What is different. If you're as burnt out as me right now, the first one is missing the
s
. -
Madara's Ghost over 11 years@Tom: I think it's time for bed :)
-
Phil over 11 yearsI just spent 2 hours learning what I already knew about
OOP
to have this exact issue. +1 -
Italo André over 11 years@JimFell everyone've been there. Try to use a text editor that sets a different color for magic methods. In my case, I use Sublime Text 2 and it gets easier to see a typo because of the color scheme.
-
Julian F. Weinert almost 10 yearsFunny, I accidentally make the same type over and over again...
-
Madara's Ghost almost 10 years@Julian: Get a better IDE.
-
Pipe about 8 yearsI had the same problem... reviewed my code and found the same typo hahahaha. Thank you :)