[wp-trac] [WordPress Trac] #31985: WP_Network class

WordPress Trac noreply at wordpress.org
Wed Jun 3 16:06:38 UTC 2015


#31985: WP_Network class
--------------------------------+------------------------
 Reporter:  johnjamesjacoby     |       Owner:
     Type:  enhancement         |      Status:  new
 Priority:  normal              |   Milestone:  4.3
Component:  Networks and Sites  |     Version:  3.0
 Severity:  normal              |  Resolution:
 Keywords:  dev-feedback        |     Focuses:  multisite
--------------------------------+------------------------

Comment (by jacobsantos):

 Replying to [comment:19 spacedmonkey]:
 > Been looking at this work and looks great. Have some feedback, so here
 we go.
 >
 > Been looking at this I feel like this class should be little more
 filterable. I know it might be something you want to worry about at the
 end, I thought I would bring it up now.

 PHP5 objects `__construct()` can not return. What this means is that
 return does not do anything as `$obj = new Whatever;` will always give an
 instance of `Whatever`, even if all `__construct()` has is `return null`.

 Furthermore, returning a new instance of an object, does not and will not
 replace the current instance of that object.

 `__construct()` having `return self::$instance` does nothing.

 What you are looking for is a factory.

 {{{
 #!php
 public static function factory() {
         $network = apply_filters( 'pre_get_network', $network );
         // Bail if not populating from an existing network
         if ( empty( $network ) ) {
                 return $network;
         }
         return new static; // static is a PHP5.3 feature, need to do
 __CLASS__(); or similar.
 }
 }}}
 >
 > Firstly here
 >
 > {{{
 >  public function __construct( $network = false ) {
 >       // Bail if not populating from an existing network
 >       if ( empty( $network ) ) {
 >                return;
 >       }
 > }}}
 >
 > How about
 >
 > {{{
 >  public function __construct( $network = false ) {
 >         $network = apply_filters( 'pre_get_network', $network );
 >       // Bail if not populating from an existing network
 >       if ( empty( $network ) ) {
 >                return $network;
 >       }
 > }}}

 See above for why this code will not work. Also this:
 http://stackoverflow.com/questions/5622605/php-getting-a-reference-
 through-a-constructor and it does appear this behavior is not documented
 on the `__construct` and `__destruct` PHP.net page, but I don't believe
 any OOP language allows you to violate OO paradigm by returning a
 different instance. Well, there might be, but it would be undocumented and
 something that a programmer would not want to do.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31985#comment:20>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list