PHP: Create instance of subclass within static method in abstract superclass? [duplicate]












0















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?










share|improve this 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.




















    0















    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?










    share|improve this 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.


















      0












      0








      0








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      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.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          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);
          }
          }

          }





          share|improve this answer

















          • 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










          • Thanks this is even better!
            – Blackbam
            Nov 12 '18 at 18:17




















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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);
          }
          }

          }





          share|improve this answer

















          • 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










          • Thanks this is even better!
            – Blackbam
            Nov 12 '18 at 18:17


















          0














          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);
          }
          }

          }





          share|improve this answer

















          • 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










          • Thanks this is even better!
            – Blackbam
            Nov 12 '18 at 18:17
















          0












          0








          0






          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);
          }
          }

          }





          share|improve this answer












          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);
          }
          }

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 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
















          • 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










          • 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





          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues