|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-08-02 11:26 UTC] nikic@php.net
-Package: PHP Language Specification
+Package: *General Issues
[2017-08-04 22:56 UTC] stas@php.net
-Status: Open
+Status: Suspended
[2017-08-04 22:56 UTC] stas@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 15 03:00:01 2025 UTC |
Description: ------------ While a lot of classes written for PHP tend towards one class per file, it is often still useful to keep supporting classes within the same file. For example, say I have an abstract type `Object`, with two concrete variations; `Object_Wrapper` and `Object_Instance`. If these are only obtained through static methods on `Object`, then there is no need to separate these out into their own files, as they should never be called for directly (i.e- all interaction occurs via `Object`). However, because of PHP's visibility model, interaction between these types may still require public methods that the developer doesn't actually want to be public. What is really needed is the ability to specify a file-private visibility type, making a method/property/constant visible only to other code/types from the same file. In the example code below, both `Object_Wrapper` and `Object_Instance` have public constructors, but shouldn't really need to, i.e- it would be preferable if these were limited to the same file only, to prevent accidental creation of one of these directly, rather than through `Object` indirectly as designed. Test script: --------------- <?php abstract class Object { public static function create() { return new Object_Wrapper(new Object_Instance()); } } /** Wraps some useful functionality around an Object_Instance, e.g- if the instance provides some baseline functionality, with wrappers providing more specifics. */ class Object_Wrapper extends Object { private $object; /** @internal */ function __construct(Object_Instance $object) { $this->object = $object; } } class Object_Instance extends Object { /** @internal */ function __construct() { /* Object is pretty useless right now */ } }