I have some classes set up as this, where -> means ‘extends’:
DBObject -> Article -> Activity
DBObject contains a generic __construct to easily load my objects from the database (hence DataBaseObject).
Article overrides this __construct, for some specialized constructor behaviour.
Activity does not implement any __construct, as it can be handled by its superclass’ __construct (Article::__construct).
However, for some reason, if I call
$activity = new Activity($args);
It ends up in DBObject::__construct, and passes Article’s one all together. I always thought that calls on a subclass were supposed to travel up the subclass line one class at a time. Am I mistaken in thinking that?
EDIT: Here’s a code snippet: http://pastebin.com/SXpSNVMm. I removed all non-necessary code. I’m calling it like this:
$userId = 60;
$title = "TestTitle";
$contents = "Lorem ipsum dolor sit amet";
$date = 1356173771;
echo "creating new activity\n";
$a = new Activity($userId, $title, $contents, $date);
Placing echo’s in the constructors revealed that Article::__construct() was not used and it went right to DBObject::__construct().
Edit 2: This is a version that should be working properly: http://ideone.com/VJzdI3 . I’m using PHPUnit for testing. This is the output if i run with PHPUnit:
creating new activity
DBObject constructed called
QUERY: 60 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '60' at line 1
The latter means it’s trying to initialize it using meekrodb; That should only happen if i call new on a subclass of DBObject with something other than null or an array. However, since Article should override __construct, that one should be called first.
Your understanding is correct: with the setup you describe creating a new
Activitywould definitely “defer” toArticle::__construct. If it does not then your description and the actual code have to differ somewhere. PHP has its more than fair share of bugs, but this is a very simple scenario to attribute the surprising behavior to buggy code.If you still think there is nothing wrong with the code, please post a working sample on an online codepad like htpp://ideone.com that exhibits the issue.