PHP late static bindings (new static): How to ensure subclass constructor compatibility and handle divergent...












0















For some more complicated class hierarchy I was playin around with a minimal example for this problem a bit.



This class is given - the method "createOrUpdate()" may be modified:



class A {

protected $a;
protected $b;
protected $c;

function __construct($a,$b,$c) {
$this->a = $a;
$this->b = $b;
$this->c = $c;
}

public static function createOrUpdate($a,$b,$c) {
if(self::exists($b)) {
someWhateverUpdate();
} else {
new static($a,$b,$c);
}
}
}


Now lets see what happens if we extend it:



class B extends A {
function __construct($a,$b,$c) {
parent::__construct($a,$b,$c);
}
}
B::createOrUpdate("rock","this","now");



Works fine!




class C extends B {
function __construct($a,$b) {
parent::__construct($a,$b,"exactly");
}
}
C::createOrUpdate("rock","this","now");



Works also fine however if somebody does createOrUpdate() parameter $c
is lost silently!




class D extends A {
protected $d;
function __construct($a,$b,$c,$d) {
$this->a = $a;
$this->b = $b;
$this->c = $c;
$this->d = $d;
}
}
D::createOrUpdate("rock","this","now");



Error: Throws ArgumentCountError




class E extends A {
function __construct($b,$c) {
$this->a = "Lorem";
$this->b = $b;
$this->c = $c;
}
}

D::createOrUpdate("rock","this","now");



Error: Works but will behave completely unexpected.




Now my question is: Can I use some reflection within createOrUpdate() in order to check if the current subclass called is implementing the constructor correctly? How would you handle this if somebody else may implement further subclasses within the hierarchy?










share|improve this question





























    0















    For some more complicated class hierarchy I was playin around with a minimal example for this problem a bit.



    This class is given - the method "createOrUpdate()" may be modified:



    class A {

    protected $a;
    protected $b;
    protected $c;

    function __construct($a,$b,$c) {
    $this->a = $a;
    $this->b = $b;
    $this->c = $c;
    }

    public static function createOrUpdate($a,$b,$c) {
    if(self::exists($b)) {
    someWhateverUpdate();
    } else {
    new static($a,$b,$c);
    }
    }
    }


    Now lets see what happens if we extend it:



    class B extends A {
    function __construct($a,$b,$c) {
    parent::__construct($a,$b,$c);
    }
    }
    B::createOrUpdate("rock","this","now");



    Works fine!




    class C extends B {
    function __construct($a,$b) {
    parent::__construct($a,$b,"exactly");
    }
    }
    C::createOrUpdate("rock","this","now");



    Works also fine however if somebody does createOrUpdate() parameter $c
    is lost silently!




    class D extends A {
    protected $d;
    function __construct($a,$b,$c,$d) {
    $this->a = $a;
    $this->b = $b;
    $this->c = $c;
    $this->d = $d;
    }
    }
    D::createOrUpdate("rock","this","now");



    Error: Throws ArgumentCountError




    class E extends A {
    function __construct($b,$c) {
    $this->a = "Lorem";
    $this->b = $b;
    $this->c = $c;
    }
    }

    D::createOrUpdate("rock","this","now");



    Error: Works but will behave completely unexpected.




    Now my question is: Can I use some reflection within createOrUpdate() in order to check if the current subclass called is implementing the constructor correctly? How would you handle this if somebody else may implement further subclasses within the hierarchy?










    share|improve this question



























      0












      0








      0








      For some more complicated class hierarchy I was playin around with a minimal example for this problem a bit.



      This class is given - the method "createOrUpdate()" may be modified:



      class A {

      protected $a;
      protected $b;
      protected $c;

      function __construct($a,$b,$c) {
      $this->a = $a;
      $this->b = $b;
      $this->c = $c;
      }

      public static function createOrUpdate($a,$b,$c) {
      if(self::exists($b)) {
      someWhateverUpdate();
      } else {
      new static($a,$b,$c);
      }
      }
      }


      Now lets see what happens if we extend it:



      class B extends A {
      function __construct($a,$b,$c) {
      parent::__construct($a,$b,$c);
      }
      }
      B::createOrUpdate("rock","this","now");



      Works fine!




      class C extends B {
      function __construct($a,$b) {
      parent::__construct($a,$b,"exactly");
      }
      }
      C::createOrUpdate("rock","this","now");



      Works also fine however if somebody does createOrUpdate() parameter $c
      is lost silently!




      class D extends A {
      protected $d;
      function __construct($a,$b,$c,$d) {
      $this->a = $a;
      $this->b = $b;
      $this->c = $c;
      $this->d = $d;
      }
      }
      D::createOrUpdate("rock","this","now");



      Error: Throws ArgumentCountError




      class E extends A {
      function __construct($b,$c) {
      $this->a = "Lorem";
      $this->b = $b;
      $this->c = $c;
      }
      }

      D::createOrUpdate("rock","this","now");



      Error: Works but will behave completely unexpected.




      Now my question is: Can I use some reflection within createOrUpdate() in order to check if the current subclass called is implementing the constructor correctly? How would you handle this if somebody else may implement further subclasses within the hierarchy?










      share|improve this question
















      For some more complicated class hierarchy I was playin around with a minimal example for this problem a bit.



      This class is given - the method "createOrUpdate()" may be modified:



      class A {

      protected $a;
      protected $b;
      protected $c;

      function __construct($a,$b,$c) {
      $this->a = $a;
      $this->b = $b;
      $this->c = $c;
      }

      public static function createOrUpdate($a,$b,$c) {
      if(self::exists($b)) {
      someWhateverUpdate();
      } else {
      new static($a,$b,$c);
      }
      }
      }


      Now lets see what happens if we extend it:



      class B extends A {
      function __construct($a,$b,$c) {
      parent::__construct($a,$b,$c);
      }
      }
      B::createOrUpdate("rock","this","now");



      Works fine!




      class C extends B {
      function __construct($a,$b) {
      parent::__construct($a,$b,"exactly");
      }
      }
      C::createOrUpdate("rock","this","now");



      Works also fine however if somebody does createOrUpdate() parameter $c
      is lost silently!




      class D extends A {
      protected $d;
      function __construct($a,$b,$c,$d) {
      $this->a = $a;
      $this->b = $b;
      $this->c = $c;
      $this->d = $d;
      }
      }
      D::createOrUpdate("rock","this","now");



      Error: Throws ArgumentCountError




      class E extends A {
      function __construct($b,$c) {
      $this->a = "Lorem";
      $this->b = $b;
      $this->c = $c;
      }
      }

      D::createOrUpdate("rock","this","now");



      Error: Works but will behave completely unexpected.




      Now my question is: Can I use some reflection within createOrUpdate() in order to check if the current subclass called is implementing the constructor correctly? How would you handle this if somebody else may implement further subclasses within the hierarchy?







      php inheritance constructor static late-static-binding






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:31







      Blackbam

















      asked Nov 13 '18 at 14:19









      BlackbamBlackbam

      5,046124175




      5,046124175
























          2 Answers
          2






          active

          oldest

          votes


















          1














          How about if you implement interface to your A class?



          interface interfaceA {
          public function __construct($a, $b, $c);
          }

          class A implements interfaceA
          {
          ...
          }


          This will force every extending class to either not have constructor or implement the one matching on class A or PHP Fatal error: Declaration of B::__construct($a, $b) must be compatible with interfaceA::__construct($a, $b, $c) will be thrown.



          You can also add public static function createOrUpdate($a, $b, $c); to the interface to force all extending classes to implement such method.






          share|improve this answer


























          • Thanks I like the idea on putting an interface on top of the hierarchy. Making createOrUpdate abstract is also a good idea if code reuse can be neglected however in this case I want it.

            – Blackbam
            Nov 13 '18 at 14:45



















          0














          After experimenting a bit further also with the input of @nforced I found another good solution - make the constructor protected and final. Now subclasses are forced to implement a creation method (e.g. create()) which is using the desired constructor.



          class A {

          protected $a;
          protected $b;
          protected $c;

          protected final function __construct($a,$b,$c) {
          $this->a = $a;
          $this->b = $b;
          $this->c = $c;
          }

          public static function createOrUpdate($a,$b,$c) {
          if(self::exists($b)) {
          updateMe();
          } else {
          new static($a,$b,$c);
          }
          }
          }





          share|improve this answer
























          • You can also do abstract class A with abstract public static function createOrUpdate($a,$b,$c); to force subclasses to implement this method.

            – nforced
            Nov 13 '18 at 15:10











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283075%2fphp-late-static-bindings-new-static-how-to-ensure-subclass-constructor-compat%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          How about if you implement interface to your A class?



          interface interfaceA {
          public function __construct($a, $b, $c);
          }

          class A implements interfaceA
          {
          ...
          }


          This will force every extending class to either not have constructor or implement the one matching on class A or PHP Fatal error: Declaration of B::__construct($a, $b) must be compatible with interfaceA::__construct($a, $b, $c) will be thrown.



          You can also add public static function createOrUpdate($a, $b, $c); to the interface to force all extending classes to implement such method.






          share|improve this answer


























          • Thanks I like the idea on putting an interface on top of the hierarchy. Making createOrUpdate abstract is also a good idea if code reuse can be neglected however in this case I want it.

            – Blackbam
            Nov 13 '18 at 14:45
















          1














          How about if you implement interface to your A class?



          interface interfaceA {
          public function __construct($a, $b, $c);
          }

          class A implements interfaceA
          {
          ...
          }


          This will force every extending class to either not have constructor or implement the one matching on class A or PHP Fatal error: Declaration of B::__construct($a, $b) must be compatible with interfaceA::__construct($a, $b, $c) will be thrown.



          You can also add public static function createOrUpdate($a, $b, $c); to the interface to force all extending classes to implement such method.






          share|improve this answer


























          • Thanks I like the idea on putting an interface on top of the hierarchy. Making createOrUpdate abstract is also a good idea if code reuse can be neglected however in this case I want it.

            – Blackbam
            Nov 13 '18 at 14:45














          1












          1








          1







          How about if you implement interface to your A class?



          interface interfaceA {
          public function __construct($a, $b, $c);
          }

          class A implements interfaceA
          {
          ...
          }


          This will force every extending class to either not have constructor or implement the one matching on class A or PHP Fatal error: Declaration of B::__construct($a, $b) must be compatible with interfaceA::__construct($a, $b, $c) will be thrown.



          You can also add public static function createOrUpdate($a, $b, $c); to the interface to force all extending classes to implement such method.






          share|improve this answer















          How about if you implement interface to your A class?



          interface interfaceA {
          public function __construct($a, $b, $c);
          }

          class A implements interfaceA
          {
          ...
          }


          This will force every extending class to either not have constructor or implement the one matching on class A or PHP Fatal error: Declaration of B::__construct($a, $b) must be compatible with interfaceA::__construct($a, $b, $c) will be thrown.



          You can also add public static function createOrUpdate($a, $b, $c); to the interface to force all extending classes to implement such method.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 14:53

























          answered Nov 13 '18 at 14:35









          nforcednforced

          91112




          91112













          • Thanks I like the idea on putting an interface on top of the hierarchy. Making createOrUpdate abstract is also a good idea if code reuse can be neglected however in this case I want it.

            – Blackbam
            Nov 13 '18 at 14:45



















          • Thanks I like the idea on putting an interface on top of the hierarchy. Making createOrUpdate abstract is also a good idea if code reuse can be neglected however in this case I want it.

            – Blackbam
            Nov 13 '18 at 14:45

















          Thanks I like the idea on putting an interface on top of the hierarchy. Making createOrUpdate abstract is also a good idea if code reuse can be neglected however in this case I want it.

          – Blackbam
          Nov 13 '18 at 14:45





          Thanks I like the idea on putting an interface on top of the hierarchy. Making createOrUpdate abstract is also a good idea if code reuse can be neglected however in this case I want it.

          – Blackbam
          Nov 13 '18 at 14:45













          0














          After experimenting a bit further also with the input of @nforced I found another good solution - make the constructor protected and final. Now subclasses are forced to implement a creation method (e.g. create()) which is using the desired constructor.



          class A {

          protected $a;
          protected $b;
          protected $c;

          protected final function __construct($a,$b,$c) {
          $this->a = $a;
          $this->b = $b;
          $this->c = $c;
          }

          public static function createOrUpdate($a,$b,$c) {
          if(self::exists($b)) {
          updateMe();
          } else {
          new static($a,$b,$c);
          }
          }
          }





          share|improve this answer
























          • You can also do abstract class A with abstract public static function createOrUpdate($a,$b,$c); to force subclasses to implement this method.

            – nforced
            Nov 13 '18 at 15:10
















          0














          After experimenting a bit further also with the input of @nforced I found another good solution - make the constructor protected and final. Now subclasses are forced to implement a creation method (e.g. create()) which is using the desired constructor.



          class A {

          protected $a;
          protected $b;
          protected $c;

          protected final function __construct($a,$b,$c) {
          $this->a = $a;
          $this->b = $b;
          $this->c = $c;
          }

          public static function createOrUpdate($a,$b,$c) {
          if(self::exists($b)) {
          updateMe();
          } else {
          new static($a,$b,$c);
          }
          }
          }





          share|improve this answer
























          • You can also do abstract class A with abstract public static function createOrUpdate($a,$b,$c); to force subclasses to implement this method.

            – nforced
            Nov 13 '18 at 15:10














          0












          0








          0







          After experimenting a bit further also with the input of @nforced I found another good solution - make the constructor protected and final. Now subclasses are forced to implement a creation method (e.g. create()) which is using the desired constructor.



          class A {

          protected $a;
          protected $b;
          protected $c;

          protected final function __construct($a,$b,$c) {
          $this->a = $a;
          $this->b = $b;
          $this->c = $c;
          }

          public static function createOrUpdate($a,$b,$c) {
          if(self::exists($b)) {
          updateMe();
          } else {
          new static($a,$b,$c);
          }
          }
          }





          share|improve this answer













          After experimenting a bit further also with the input of @nforced I found another good solution - make the constructor protected and final. Now subclasses are forced to implement a creation method (e.g. create()) which is using the desired constructor.



          class A {

          protected $a;
          protected $b;
          protected $c;

          protected final function __construct($a,$b,$c) {
          $this->a = $a;
          $this->b = $b;
          $this->c = $c;
          }

          public static function createOrUpdate($a,$b,$c) {
          if(self::exists($b)) {
          updateMe();
          } else {
          new static($a,$b,$c);
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 14:59









          BlackbamBlackbam

          5,046124175




          5,046124175













          • You can also do abstract class A with abstract public static function createOrUpdate($a,$b,$c); to force subclasses to implement this method.

            – nforced
            Nov 13 '18 at 15:10



















          • You can also do abstract class A with abstract public static function createOrUpdate($a,$b,$c); to force subclasses to implement this method.

            – nforced
            Nov 13 '18 at 15:10

















          You can also do abstract class A with abstract public static function createOrUpdate($a,$b,$c); to force subclasses to implement this method.

          – nforced
          Nov 13 '18 at 15:10





          You can also do abstract class A with abstract public static function createOrUpdate($a,$b,$c); to force subclasses to implement this method.

          – nforced
          Nov 13 '18 at 15:10


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283075%2fphp-late-static-bindings-new-static-how-to-ensure-subclass-constructor-compat%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini