Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One primary need I have for late static binding is for a set of static instance-creation methods. </p> <p>This <a href="http://harmoni.sourceforge.net/harmoniDoc/phpdoc/harmoni/primitives.chronology/DateAndTime.html" rel="nofollow noreferrer">DateAndTime class</a> is part of a chronology library that I ported to PHP from Smalltalk/Squeak. Using static instance-creation methods enables creation of instances with a variety of argument types, while keeping parameter checking in the static method so that the consumer of the library is unable to obtain an instance that is not fully valid. </p> <p>Late static binding is useful in this case so that the implementations of these static instance-creation methods can determine what class was originally targeted by the call. Here is an example of usage:</p> <p><strong>With LSB:</strong></p> <pre><code>class DateAndTime { public static function now() { $class = static::myClass(); $obj = new $class; $obj-&gt;setSeconds(time()); return $obj; } public static function yesterday() { $class = static::myClass(); $obj = new $class; $obj-&gt;setSeconds(time() - 86400); return $obj; } protected static function myClass () { return 'DateAndTime'; } } class Timestamp extends DateAndTime { protected static function myClass () { return 'Timestamp'; } } // Usage: $date = DateAndTime::now(); $timestamp = Timestamp::now(); $date2 = DateAndTime::yesterday(); $timestamp2 = Timestamp::yesterday(); </code></pre> <p>Without late static binding, [as in my current implementation] each class must implement every instance creation method as in this example:</p> <p><strong>Without LSB:</strong></p> <pre><code>class DateAndTime { public static function now($class = 'DateAndTime') { $obj = new $class; $obj-&gt;setSeconds(time()); return $obj; } public static function yesterday($class = 'DateAndTime') { $obj = new $class; $obj-&gt;setSeconds(time() - 86400); return $obj; } } class Timestamp extends DateAndTime { public static function now($class = 'Timestamp') { return self::now($class); } public static function yesterday($class = 'Timestamp') { return self::yesterday($class); } } </code></pre> <p>As the number of instance-creation methods and class-hierarchy increases the duplication of methods becomes a real pain in the butt. LSB reduces this duplication and allows for much cleaner and more straight-forward implementations.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload