General Question

Vincentt's avatar

How can I refer to a variable or function defined by a class child?

Asked by Vincentt (8074points) February 4th, 2008

In PHP, I’ve created an abstract class. This class is supposed to be able to compare a given version number with a number that has been set by its children.

Now, I tried referring to self::VERSION, which is a class constant that its children define. However, I could understand PHP not being able to do this because it cannot be sure that the constant is defined.

However, I then tried to add an abstract function getVersion(), so that you could be sure that children classes would have that function defined. However, when calling self::getVersion(), PHP complains about not being able to call an abstract function. In other words: when the parent class calls self, in an instance of a children class that will refer to the parent.

Now, is there a way for me to refer to a variable of method of a child class?


Observing members: 0 Composing members: 0

8 Answers

segdeha's avatar

Could you make it a public static variable and call it by Classname::Varname ?

paulc's avatar

Your question is a bit confusing but I think I know what you’re getting at: You want to store a given version value in the abstract class and make it comparable to a different value in the child class(es).

I’m unsure what context you’re doing this in but here is my suggestion. If you keep the abstract class’ version property private and declare getVersion to return the value you can then have your children override the getVersion method to return their own version value. You can then have a method within the abstract class like compareVersions which can compare its own private value against the overridden value returned by the child class implementation of getVersion.

Forgive my PHP, it has been a while since I’ve done anything in it…

class AbstractClass {
private $version = 1.0;

function get_version() {
return $this->version;

function compare_versions() {
return $this->version == get_version();

class DaughterClass {
private $version = 1.1;

function getVersion() {
return $this->version;

So calling compare_versions() on an instance of a DaughterClass would return false in this case.

Side note: fluther needs block code formatting, I can only get code to work

paulc's avatar

The method in the DaughterClass should read get_version() not getVersion().

Vincentt's avatar

@segdeha – no, that’s not possible, as the parent class does not know Classname. Every class could extend the parent class so there’s a whole plethora of possible names…

@paulc – yes, I agree, I had extreme difficulty formulating my question, more so because code formatting is so difficult at Fluther.

Anyway, you got me just the other way around: I want to store a given value in the child class and create a function in the abstract class to compare values.

Also, I now realize I forgot to mention one thing: the abstract class’ compare() method is static, meaning I can’t use $this. I’ve made it difficult for myself, haven’t I? ;-)

Thanks, Vincent.

segdeha's avatar

Your next-to-last sentence may be your clue to a solution. If it’s this difficult to do what you want, then you may want to re-evaluate the structure of your program.

paulc's avatar

@Vincentt – actually I think I did get it but I’m suggesting you exploit the fact that you can overload methods to do what you’re trying to do. I forgot to show that DaughterClass subclasses AbstractClass but I think it was obvious. You could instead keep the version as a static constant for both abstract and its children however, your compare method would not be able to be static in my example.

In any case, segdeha is right: you might want to check yourself now before you go too far. It sounds to me like you may have found your first “broken window” so you should fix that before it snowballs.

Vincentt's avatar

@paulc – yeah, that won’t really work because I cannot call the overloaded method using $this.

And I agree that this is a typical case of something so difficult that I’m probably doing it the wrong way, but for now I don’t really know of an alternative way. However, I just recall a similar class in a framework that I’ll check out at home, and I hope that they’ve found a clever way to do this. Thanks anyway.

Vincentt's avatar

OK, I did some more digging, and apparently, I’m not the only one with this problem, judging by the comments on PHP’s get_class() man page. The solutions all are far from ideal, ranging from the use of Singletons to resorting to use $this.

I guess for now I’ll settle with requiring the child class to override the methods and pass them its own name so that can be used to retrieve certain values. Let’s just hope PHP’s special keyword self will be made to act more logically in the future…

Answer this question




to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther