rfc:nested_classes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:nested_classes [2013/10/01 22:26] – update krakjoe | rfc:nested_classes [2013/10/13 18:59] – [PHP RFC: Nested Classes] krakjoe | ||
---|---|---|---|
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: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
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 | + | * protected - the class may be access |
- | INSERT DIAGRAMS HERE | + | ===== Private Classes ===== |
- | ===== Syntax Example ===== | + | The following example shows how to blackbox some of your functionality in a private nested class: |
- | The following is a syntax example and use case example; the primary use case for nested classes is black boxing: a nested | + | <code php> |
+ | <?php | ||
+ | /* | ||
+ | * \foo | ||
+ | * @package \foo | ||
+ | */ | ||
+ | class foo | ||
+ | { | ||
+ | /* | ||
+ | * \foo\bar supporting | ||
+ | * @subpackage \foo\bar | ||
+ | * @private | ||
+ | */ | ||
+ | private | ||
+ | { | ||
+ | public | ||
+ | /* ... */ | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /* PUBLIC | ||
+ | |||
+ | | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
- | In the following | + | var_dump(new \foo()); |
+ | ?> | ||
+ | </ | ||
+ | |||
+ | In the example '' | ||
+ | |||
+ | Attempting: | ||
<code php> | <code php> | ||
- | <?php | + | var_dump(new \foo\bar()); |
- | namespace io { | + | </ |
- | | + | will result in |
- | | + | |
+ | < | ||
+ | Fatal error: Cannot access private | ||
+ | </ | ||
+ | |||
+ | Private classes are very private, the following example demonstrates this: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | /* | ||
+ | * foo | ||
+ | * @package foo | ||
+ | */ | ||
+ | class foo | ||
+ | { | ||
+ | /* | ||
+ | * foo\bar supporting class for foo | ||
+ | * @subpackage foo\bar | ||
+ | * @private | ||
+ | */ | ||
+ | private class bar | ||
+ | | ||
| | ||
- | | + | |
- | | + | |
- | | + | |
- | + | | |
- | | + | |
- | private class Buffer implements FileReader\IOBuffer { | + | private class baz |
- | /* ... */ | + | { |
- | } | + | |
| | ||
public function __construct() { | public function __construct() { | ||
- | | + | |
- | } | + | } |
} | } | ||
| | ||
- | | + | |
- | private class Buffer implements FileReader\IOBuffer | + | |
- | /* ... */ | + | } |
- | } | + | } |
- | + | ||
+ | /* PUBLIC API METHODS HERE */ | ||
+ | |||
+ | public function __construct() | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | new \foo(); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Output: | ||
+ | |||
+ | < | ||
+ | Fatal error: Cannot access | ||
+ | </ | ||
+ | |||
+ | ===== 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 | ||
+ | | ||
+ | | ||
+ | { | ||
+ | |||
+ | /* | ||
+ | * \foo\bar\baz supporting class for foo\bar | ||
+ | * @subpackage foo\bar\baz | ||
+ | * @protected | ||
+ | */ | ||
+ | protected class baz | ||
+ | { | ||
+ | |||
public function __construct() { | public function __construct() { | ||
- | $this-> | + | |
} | } | ||
} | } | ||
- | | + | |
- | public function __construct($filename) { | + | public function __construct() { |
- | | + | $this->baz = new foo\bar\baz(); |
- | case FileReader:: | + | |
- | | + | |
- | } break; | + | |
- | + | ||
- | case FileReader:: | + | |
- | $this-> | + | |
- | } break; | + | |
- | + | ||
- | /* ... */ | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public static function getFileType($filename) { | + | |
- | /** .. **/ | + | |
- | return FileReader:: | + | |
} | } | ||
+ | } | ||
+ | |||
+ | /* PUBLIC API METHODS HERE */ | ||
+ | |||
+ | public function __construct() | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
} | } | ||
} | } | ||
- | namespace { | + | var_dump(new \foo()); |
- | | + | </ |
+ | |||
+ | Output: | ||
+ | |||
+ | < | ||
+ | object(foo)# | ||
+ | | ||
+ | object(foo\bar)# | ||
+ | [" | ||
+ | object(foo\bar\baz)# | ||
+ | } | ||
+ | } | ||
+ | [" | ||
+ | object(foo\bar\baz)#4 (0) { | ||
+ | } | ||
} | } | ||
- | ?> | ||
</ | </ | ||
- | More examples to come as the questions come in ... | + | The protected class '' |
+ | <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-> | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | /* | ||
+ | * \foo\qux supporting class for foo | ||
+ | */ | ||
+ | private class qux extends foo\bar\baz | ||
+ | { | ||
+ | public function __construct() { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /* PUBLIC API METHODS HERE */ | ||
+ | |||
+ | public function __construct() | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | var_dump(new \foo()); | ||
+ | </ | ||
+ | |||
+ | Output: | ||
+ | |||
+ | < | ||
+ | object(foo)# | ||
+ | [" | ||
+ | object(foo\bar)# | ||
+ | [" | ||
+ | object(foo\bar\baz)# | ||
+ | } | ||
+ | } | ||
+ | [" | ||
+ | object(foo\bar\baz)# | ||
+ | } | ||
+ | [" | ||
+ | object(foo\qux)# | ||
+ | } | ||
+ | } | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
rfc/nested_classes.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1