Hi everyone,
Quite a difficult problem here. I need help confirming if this is a bug in PHP5 or not, as I think the following code produces an incorrect result. However, I am open to education if I have assumed that this result is false.
Basically, I have a base class which contains a static method. The static method should return an instance of the class. If I use the base class to extend from, I would assume that the static method would return an instance of the derived class, NOT an instance of the base class:
<?php
class BaseClass {
static public function fetch() {
return new self;
}
}
class ConcreteClass extends BaseClass {
private $title;
public function setTitle( $newTitle ) {
$this->title = $newTitle;
}
}
$c = ConcreteClass::fetch();
var_dump($c);
$c->setTitle('My Title');
?>
This results in $c containing an instance of BaseClass, not, as I assume, an instance of ConcreteClass.
Am I wrong here or is this a bug?
Cheers!
<!-- m --><a class="postlink" href="Jalfhttp://bugs.php.net/bug.php?id=30934">Jalfhttp://bugs.php.net/bug.php?id=30934</a><!-- m -->
Thread on php bugs on a similar note ^^
see bottom of article for explanation - i think its similar to what your askingThanks for that! This is exactly the same issue.
So, this is not a bug, eh? I would have thought that it was obvious what the expected behaviour of this would be, but I guess I'm wrong. I guess this really IS a feature, and that me expecting things to work in an obvious manner is what is at fault here.
This is stupid. This is clearly a case of optimisation before implementation: they haven't implemented this correctly prefering to opt for speed rather than completeness. Nice one. No wonder people see PHP as a joke.
Sorry for the rant, but as much as I love PHP, sometimes the dev's are so boneheaded. Sometimes they don't do PHP any favours!I'm wondering how this would be done in, say, C#, which as far as I recall doesn't have anything with the semantics of PHP's "self". And of course you have to declare a return type for the fetch method.
I guess I'm wrong.You're wrong, because you've forgotten what "static" means. <!-- m --><a class="postlink" href="http://foldoc.org/foldoc/foldoc.cgi?static">http://foldoc.org/foldoc/foldoc.cgi?static</a><!-- m -->, <!-- m --><a class="postlink" href="http://foldoc.org/foldoc/foldoc.cgi?static+typing@Weedpacket">http://foldoc.org/foldoc/foldoc.cgi?sta ... Weedpacket</a><!-- m -->: You are 100% correct. Static typing is implemented at Compile time, not Run-time, hence why this isn't inherited as expected. Please ignore my bone-headed rant at PHP!
I'm gonna slink off and hid in my hole now.... I just wonder why someone would like to do that instead of just creating a new ConcreteClass the normal way.I'm gonna slink off and hid in my hole now....
I've heard tell that this early binding of self is to be addressed in PHP6 by adding a late-binding alternative.
Quite a difficult problem here. I need help confirming if this is a bug in PHP5 or not, as I think the following code produces an incorrect result. However, I am open to education if I have assumed that this result is false.
Basically, I have a base class which contains a static method. The static method should return an instance of the class. If I use the base class to extend from, I would assume that the static method would return an instance of the derived class, NOT an instance of the base class:
<?php
class BaseClass {
static public function fetch() {
return new self;
}
}
class ConcreteClass extends BaseClass {
private $title;
public function setTitle( $newTitle ) {
$this->title = $newTitle;
}
}
$c = ConcreteClass::fetch();
var_dump($c);
$c->setTitle('My Title');
?>
This results in $c containing an instance of BaseClass, not, as I assume, an instance of ConcreteClass.
Am I wrong here or is this a bug?
Cheers!
<!-- m --><a class="postlink" href="Jalfhttp://bugs.php.net/bug.php?id=30934">Jalfhttp://bugs.php.net/bug.php?id=30934</a><!-- m -->
Thread on php bugs on a similar note ^^
see bottom of article for explanation - i think its similar to what your askingThanks for that! This is exactly the same issue.
So, this is not a bug, eh? I would have thought that it was obvious what the expected behaviour of this would be, but I guess I'm wrong. I guess this really IS a feature, and that me expecting things to work in an obvious manner is what is at fault here.
This is stupid. This is clearly a case of optimisation before implementation: they haven't implemented this correctly prefering to opt for speed rather than completeness. Nice one. No wonder people see PHP as a joke.
Sorry for the rant, but as much as I love PHP, sometimes the dev's are so boneheaded. Sometimes they don't do PHP any favours!I'm wondering how this would be done in, say, C#, which as far as I recall doesn't have anything with the semantics of PHP's "self". And of course you have to declare a return type for the fetch method.
I guess I'm wrong.You're wrong, because you've forgotten what "static" means. <!-- m --><a class="postlink" href="http://foldoc.org/foldoc/foldoc.cgi?static">http://foldoc.org/foldoc/foldoc.cgi?static</a><!-- m -->, <!-- m --><a class="postlink" href="http://foldoc.org/foldoc/foldoc.cgi?static+typing@Weedpacket">http://foldoc.org/foldoc/foldoc.cgi?sta ... Weedpacket</a><!-- m -->: You are 100% correct. Static typing is implemented at Compile time, not Run-time, hence why this isn't inherited as expected. Please ignore my bone-headed rant at PHP!
I'm gonna slink off and hid in my hole now.... I just wonder why someone would like to do that instead of just creating a new ConcreteClass the normal way.I'm gonna slink off and hid in my hole now....
I've heard tell that this early binding of self is to be addressed in PHP6 by adding a late-binding alternative.