ncoo
asked on
PHP Abstract Class and Self
How do you go about creating a method which returns self(), but the self returned is the self in the extending class and not the extended class.
The following will give an error:
Fatal error: Cannot instantiate abstract class xyz
I know I could add the method to abc, but wanted to know if there's a better way.
The following will give an error:
Fatal error: Cannot instantiate abstract class xyz
I know I could add the method to abc, but wanted to know if there's a better way.
abstract class xyz {
function method() {
return new self();
}
}
class abc extends xyz {
}
$abc = new abc();
$abc2 = $abc->method();
may be you should try $this instead of self
you need to study more about Oops in PHP
Abstract class does not allow objects and in your code you are trying to create an object of the abstract class. Secondly, methods are only defined in a abstract class they are not initialized. You should not right functioning of a function in the abstract class.
Read more there:
http://php.net/manual/en/language.oop5.abstract.php
Abstract class does not allow objects and in your code you are trying to create an object of the abstract class. Secondly, methods are only defined in a abstract class they are not initialized. You should not right functioning of a function in the abstract class.
Read more there:
http://php.net/manual/en/language.oop5.abstract.php
You cannot instantiate an abstract class. The "new" operator in "return new self()" is trying to instantiate a xyz and that is why you get the message.
ASKER
I know why I get the message I'm just asking is there any way to do a new self(); where the self() is the extending class, abc and not the extended class, xyz.
So when I call method it returns a new abc class even though the method is in xyz.
I know I can't use new self() but I only use it to try and illustrate what I am trying to do.
All I want to do is have method return a new class of what ever is extending the class.
So when I call method it returns a new abc class even though the method is in xyz.
I know I can't use new self() but I only use it to try and illustrate what I am trying to do.
All I want to do is have method return a new class of what ever is extending the class.
yes you can achieve it like following:
class xyz {
function method() {
return new self();
}
}
class abc extends xyz {
}
$abc = new abc();
$abc2 = $abc->method();
var_dump($abc2);
but the only difference you need is to remove keyword 'abstract'. You can not create object of abstract class. That is the whole point.
class xyz {
function method() {
return new self();
}
}
class abc extends xyz {
}
$abc = new abc();
$abc2 = $abc->method();
var_dump($abc2);
but the only difference you need is to remove keyword 'abstract'. You can not create object of abstract class. That is the whole point.
ASKER
The problem with that is self returns the extended class not the extending class.
So in that code $abc2 is of object type xyz.
What I would like to happend is $abc2 to be of object type abc.
I know I can move the method to abc and achieve this but I want to keep the method in xyz or otherwise everything that extends xyz will have to have the same method.
So in that code $abc2 is of object type xyz.
What I would like to happend is $abc2 to be of object type abc.
I know I can move the method to abc and achieve this but I want to keep the method in xyz or otherwise everything that extends xyz will have to have the same method.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.