rfc:nested_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:nested_classes [2013/10/01 23:37] – [Private Classes] krakjoerfc:nested_classes [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 4: Line 4:
   * Date: 2013-09-29   * Date: 2013-09-29
   * Author: Joe Watkins, krakjoe@php.net   * Author: Joe Watkins, krakjoe@php.net
-  * Status: Under Discussion+  * Status: Withdrawn
   * First Published at: http://wiki.php.net/rfc/nested_classes   * First Published at: http://wiki.php.net/rfc/nested_classes
  
Line 41: Line 41:
   * public - the class is accessible everywhere   * public - the class is accessible everywhere
   * private - the class may be accessed by any class declared in the //outer// class   * private - the class may be accessed by any class declared in the //outer// class
-  * protected - the class may be accessed by any class up to and including the //outermost// class+  * protected - the class may be access by any class in the same virtual namespace
  
 ===== Private Classes ===== ===== Private Classes =====
Line 134: Line 134:
     {     {
         $this->bar = new \foo\bar();         $this->bar = new \foo\bar();
-        $this->baz = new \foo\bar\baz();+        $this->baz = new \foo\bar\baz(); /* line 39 */
     }     }
 } }
Line 146: Line 146:
 <code> <code>
 Fatal error: Cannot access private class foo\bar\baz from foo in %s on line 39 Fatal error: Cannot access private class foo\bar\baz from foo in %s on line 39
 +</code>
 +
 +===== Protecting bits of your Privates =====
 +
 +The following example shows how protected and private classes can be used in conjunction to provide versatile encapsulation:
 +
 +<code php>
 +<?php
 +/*
 +* foo
 +* @package foo
 +*/
 +class foo
 +{
 +    /*
 +    * foo\bar supporting class for foo
 +    * @subpackage foo\bar
 +    * @private
 +    */
 +    private class bar
 +    {
 + 
 +        /*
 +        * \foo\bar\baz supporting class for foo\bar
 +        * @subpackage foo\bar\baz
 +        * @protected
 +        */
 +        protected class baz
 +        {
 + 
 +            public function __construct() {
 + 
 +            }
 +        }
 + 
 +        public function __construct() {
 +            $this->baz = new foo\bar\baz();
 +        }
 +    }
 + 
 +    /* PUBLIC API METHODS HERE */
 + 
 +    public function __construct()
 +    {
 +        $this->bar = new \foo\bar();
 +        $this->baz = new \foo\bar\baz();
 +    }
 +}
 +
 +var_dump(new \foo());
 +</code>
 +
 +Output:
 +
 +<code>
 +object(foo)#1 (2) {
 +  ["bar"]=>
 +  object(foo\bar)#2 (1) {
 +    ["baz"]=>
 +    object(foo\bar\baz)#3 (0) {
 +    }
 +  }
 +  ["baz"]=>
 +  object(foo\bar\baz)#4 (0) {
 +  }
 +}
 +</code>
 +
 +The protected class ''\foo\bar\baz'' can now be used in ''\foo'', for example:
 +
 +<code php>
 +<?php
 +/*
 +* foo
 +* @package foo
 +*/
 +class foo
 +{
 +    /*
 +    * foo\bar supporting class for foo
 +    * @subpackage foo\bar
 +    * @private
 +    */
 +    private class bar
 +    {
 + 
 +        /*
 +        * \foo\bar\baz supporting class for foo\bar
 +        * @subpackage foo\bar\baz
 +        * @protected
 +        */
 +        protected class baz
 +        {
 + 
 +            public function __construct() {
 + 
 +            }
 +        }
 + 
 +        public function __construct() {
 +            $this->baz = new foo\bar\baz();
 +        }
 +    }
 +    
 +    /*
 +    * \foo\qux supporting class for foo
 +    */
 +    private class qux extends foo\bar\baz
 +    {
 +        public function __construct() {
 +            
 +        }
 +    }
 + 
 +    /* PUBLIC API METHODS HERE */
 + 
 +    public function __construct()
 +    {
 +        $this->bar = new \foo\bar();
 +        $this->baz = new \foo\bar\baz();
 +        $this->qux = new \foo\qux();
 +    }
 +}
 + 
 +var_dump(new \foo());
 +</code>
 +
 +Output:
 +
 +<code>
 +object(foo)#1 (3) {
 +  ["bar"]=>
 +  object(foo\bar)#2 (1) {
 +    ["baz"]=>
 +    object(foo\bar\baz)#3 (0) {
 +    }
 +  }
 +  ["baz"]=>
 +  object(foo\bar\baz)#4 (0) {
 +  }
 +  ["qux"]=>
 +  object(foo\qux)#5 (0) {
 +  }
 +}
 </code> </code>
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/nested_classes.1380670660.txt.gz · Last modified: 2017/09/22 13:28 (external edit)