Page 1 of 1

Hw do i find the parenting class of a static child-class?

Posted: Wed Nov 05, 2008 1:23 pm
by ekampp
I have a setup, where i have a class Article that extends the class Model. The Model-class have some methods, that is called statically, but can be overwritten in the Article-class, to allow for customization, much as in RoR's model-structure. I'm calling the Article::find() which calls the Article-class, but the actual method lives in the model class, here comes the actual question: How do I in the Model-class find out which class was called statically, that extended the Model-class?

Code: Select all

# Model.php
class Model {
public static function find(){ /*Do something*/ }
}
 
# Article.php
class Article extends Model {
 
}
 
# somepage.php
$results = Article::find()

Re: Hw do i find the parenting class of a static child-class?

Posted: Wed Nov 05, 2008 1:35 pm
by requinix
You shouldn't need to.
If the parent's behavior depends on what its children are then you're not using inheritance correctly.

But to answer the question, try debug_backtrace().

Re: Hw do i find the parenting class of a static child-class?

Posted: Wed Nov 05, 2008 1:41 pm
by ekampp
Ok. Then how do you suggest i alter the code, so that the Article will inherit the Model, but you can write custom methods in the Article, which will overwrite the ones in the Model.

Re: Hw do i find the parenting class of a static child-class?

Posted: Wed Nov 05, 2008 2:03 pm
by requinix
ekampp wrote:Ok. Then how do you suggest i alter the code, so that the Article will inherit the Model, but you can write custom methods in the Article, which will overwrite the ones in the Model.
That's what happens by default.
Article extends Model. Model defines a public/protected method. If Article defines another one with the same name then calling that function will result in Article's method being executed.