|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-02-20 22:20 UTC] kalle@php.net
Description: ------------ Already now from just playing around with namespaces with the latest CVS version of the PHP_5_3 branch, I found it would be rather useful to add private classes to namespaces that internally works with the namespace and cannot be called from outside the namespace (or root namespace). Would this possible be a feature of 5.3 final? PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 07:00:01 2025 UTC |
I agree and support this addition, not just for classes but also functions. Even constants could, though I feel there is no need. On a side note, namespaces as they are right now are NOT a viable solution. Consider the following: <? final class FOO { private __construct () {} // we cannot instantiate static public $myVar; static private $otherVar; static public function MyFunc () { some code } static private function OtherFunc () { some code } } class MYVAR () { } FOO::$myVar = new MYVAR(); ?> declaring everything static in a class works pretty much like a namespace would (resolving through :: e.g. FOO::MyFunc()), which is ok but not enough, as there cannot be a class 'MYVAR' definition inside the class 'FOO' (and thus making it internal to 'FOO'), that creates the problem of having the class MYVAR accesible from everywhere and because of that the namespaces are useful, but not like they are now consider the following hypotetical syntax: <? namespace FOO { public $myVar; private $otherVar; public function MyFunc () { some code } private function OtherFunc () { some code } public function Init () { $myVar = new MYVAR(); } private class MYVAR () { } } FOO::Init(); ?> note I put 'private class', but sure it could be something like 'internal' and so on there are a couple of differences, the first being the namespace confined within brackets '{}' and thus limiting its scope, this allows for initialization code if needed in the same file the second is of course the public/private/internal/wathever support inside a namespace, so the class 'MYVAR' can only be used internally and thus being invisible to the code outside the namespace 'FOO' this is pretty handy for categorization of the properties/methods in complex/big php chunks to serve as an example: <? namespace FOO { public $tools; public $errors; public $info; internal $initialized = false; internal class TOOLS { public function CheckSomething () { ... } public function CompareSomething () { ... } ... } internal class ERRORS { public $last; public function Clear () { ... } public function Count () { ... } ... } internal class INFO { public $name; public $author; public $description; } public function Init () { if ( $initialized ) return; $tools = new TOOLS (); $errors = new ERRORS (); $info = new INFO (); $initialized = true; } } FOO::Init (); echo FOO::$info->name; echo FOO::$errors->last; ?> that would make the namespaces truly useful, if it could be done this way (or something similar) the only thing missing would be 'true' properties (readonly anyone??) with the ability to set their read/write operations much like c#... though the readonly would be already a giant step to forget about the stupid get-set pattern thanks for reading!