PHP: Create instance of subclass within static method in abstract superclass? [duplicate]
This question already has an answer here:
New self vs. new static
3 answers
How can I create an instance of a subclass within a static method within an abstract superclass in PHP?
My code is like this:
abstract class GenericUserMessage {
private $_message;
private $_type;
protected abstract function getAllMessages();
function __construct( $key, $message, string $type = NoticeTypes::INFO, $strict = false ) { // ... }
/**
* @param $key
* @param $message
* @param string $type
*/
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
new self($key,$message,$type); // here is the error
}
}
}
class UserMessage extends GenericUserMessage {
protected function getAllMessages() {
// ...
return $all_messages;
}
}
This fails with the following fatal error:
Cannot instantiate abstract class 'GenericUserMessage'
Makes sense! However I want an instance of the implementing subclass to be created. Is this possible and can it make sense for certain situations?
php inheritance static abstract-class subclass
marked as duplicate by Blackbam, Community♦ Nov 12 '18 at 20:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
New self vs. new static
3 answers
How can I create an instance of a subclass within a static method within an abstract superclass in PHP?
My code is like this:
abstract class GenericUserMessage {
private $_message;
private $_type;
protected abstract function getAllMessages();
function __construct( $key, $message, string $type = NoticeTypes::INFO, $strict = false ) { // ... }
/**
* @param $key
* @param $message
* @param string $type
*/
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
new self($key,$message,$type); // here is the error
}
}
}
class UserMessage extends GenericUserMessage {
protected function getAllMessages() {
// ...
return $all_messages;
}
}
This fails with the following fatal error:
Cannot instantiate abstract class 'GenericUserMessage'
Makes sense! However I want an instance of the implementing subclass to be created. Is this possible and can it make sense for certain situations?
php inheritance static abstract-class subclass
marked as duplicate by Blackbam, Community♦ Nov 12 '18 at 20:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
New self vs. new static
3 answers
How can I create an instance of a subclass within a static method within an abstract superclass in PHP?
My code is like this:
abstract class GenericUserMessage {
private $_message;
private $_type;
protected abstract function getAllMessages();
function __construct( $key, $message, string $type = NoticeTypes::INFO, $strict = false ) { // ... }
/**
* @param $key
* @param $message
* @param string $type
*/
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
new self($key,$message,$type); // here is the error
}
}
}
class UserMessage extends GenericUserMessage {
protected function getAllMessages() {
// ...
return $all_messages;
}
}
This fails with the following fatal error:
Cannot instantiate abstract class 'GenericUserMessage'
Makes sense! However I want an instance of the implementing subclass to be created. Is this possible and can it make sense for certain situations?
php inheritance static abstract-class subclass
This question already has an answer here:
New self vs. new static
3 answers
How can I create an instance of a subclass within a static method within an abstract superclass in PHP?
My code is like this:
abstract class GenericUserMessage {
private $_message;
private $_type;
protected abstract function getAllMessages();
function __construct( $key, $message, string $type = NoticeTypes::INFO, $strict = false ) { // ... }
/**
* @param $key
* @param $message
* @param string $type
*/
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
new self($key,$message,$type); // here is the error
}
}
}
class UserMessage extends GenericUserMessage {
protected function getAllMessages() {
// ...
return $all_messages;
}
}
This fails with the following fatal error:
Cannot instantiate abstract class 'GenericUserMessage'
Makes sense! However I want an instance of the implementing subclass to be created. Is this possible and can it make sense for certain situations?
This question already has an answer here:
New self vs. new static
3 answers
php inheritance static abstract-class subclass
php inheritance static abstract-class subclass
asked Nov 12 '18 at 18:07
Blackbam
5,031124075
5,031124075
marked as duplicate by Blackbam, Community♦ Nov 12 '18 at 20:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Blackbam, Community♦ Nov 12 '18 at 20:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Nevermind I have found a way:
abstract class GenericUserMessage {
protected abstract function instantiate($key,$message,string $type = NoticeTypes::INFO);
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
self::instantiate($key,$message,$type);
}
}
}
1
Glad to see you figured out a way to do it. Another way would have been to usenew static()
instead ofnew self()
.
– rickdenhaan
Nov 12 '18 at 18:15
Thanks this is even better!
– Blackbam
Nov 12 '18 at 18:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Nevermind I have found a way:
abstract class GenericUserMessage {
protected abstract function instantiate($key,$message,string $type = NoticeTypes::INFO);
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
self::instantiate($key,$message,$type);
}
}
}
1
Glad to see you figured out a way to do it. Another way would have been to usenew static()
instead ofnew self()
.
– rickdenhaan
Nov 12 '18 at 18:15
Thanks this is even better!
– Blackbam
Nov 12 '18 at 18:17
add a comment |
Nevermind I have found a way:
abstract class GenericUserMessage {
protected abstract function instantiate($key,$message,string $type = NoticeTypes::INFO);
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
self::instantiate($key,$message,$type);
}
}
}
1
Glad to see you figured out a way to do it. Another way would have been to usenew static()
instead ofnew self()
.
– rickdenhaan
Nov 12 '18 at 18:15
Thanks this is even better!
– Blackbam
Nov 12 '18 at 18:17
add a comment |
Nevermind I have found a way:
abstract class GenericUserMessage {
protected abstract function instantiate($key,$message,string $type = NoticeTypes::INFO);
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
self::instantiate($key,$message,$type);
}
}
}
Nevermind I have found a way:
abstract class GenericUserMessage {
protected abstract function instantiate($key,$message,string $type = NoticeTypes::INFO);
public static function createOrUpdate($key,$message,string $type = NoticeTypes::INFO) {
if(self::exists($key)) {
self::updateMessage($key,$message,$type);
} else {
self::instantiate($key,$message,$type);
}
}
}
answered Nov 12 '18 at 18:14
Blackbam
5,031124075
5,031124075
1
Glad to see you figured out a way to do it. Another way would have been to usenew static()
instead ofnew self()
.
– rickdenhaan
Nov 12 '18 at 18:15
Thanks this is even better!
– Blackbam
Nov 12 '18 at 18:17
add a comment |
1
Glad to see you figured out a way to do it. Another way would have been to usenew static()
instead ofnew self()
.
– rickdenhaan
Nov 12 '18 at 18:15
Thanks this is even better!
– Blackbam
Nov 12 '18 at 18:17
1
1
Glad to see you figured out a way to do it. Another way would have been to use
new static()
instead of new self()
.– rickdenhaan
Nov 12 '18 at 18:15
Glad to see you figured out a way to do it. Another way would have been to use
new static()
instead of new self()
.– rickdenhaan
Nov 12 '18 at 18:15
Thanks this is even better!
– Blackbam
Nov 12 '18 at 18:17
Thanks this is even better!
– Blackbam
Nov 12 '18 at 18:17
add a comment |