deathbeam Posted September 10, 2014 Share Posted September 10, 2014 Only thing I really miss from PHP is "internal" access modifier. Before I was making libraries and extensions in C# (like game frameworks, Tiled implementations, GUI loaders). Now, when I am working on my own PHP framework, I really miss internal, I was using it a lot in C#. Is internal planned to be added in PHP 7 or is it already in PHP 5.6 or it will never be added? Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/ Share on other sites More sharing options...
Richard_Grant Posted September 10, 2014 Share Posted September 10, 2014 Not sure what your talking about but php does have access modifiers class phpfreak{ protected age; protected height; protected function getage(){ return $this->name; } protected function getheight(){ return $this->height; } } class create_freak extends phpfreak{ public function create_freak($age, $height){ $this->age = 20;//protected $this->height = 100;//Protected } public function get(){ return Array(parent::getage(), parent::getheight()); } } $create_a_freak = new create_freak();//calls constructor print_r($create_a_freak.get()); //gets values Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1490566 Share on other sites More sharing options...
deathbeam Posted September 10, 2014 Author Share Posted September 10, 2014 Not sure what your talking about but php does have access modifiers class phpfreak{ protected age; protected height; protected function getage(){ return $this->name; } protected function getheight(){ return $this->height; } } class create_freak extends phpfreak{ public function create_freak($age, $height){ $this->age = 20;//protected $this->height = 100;//Protected } public function get(){ return Array(parent::getage(), parent::getheight()); } } $create_a_freak = new create_freak();//calls constructor print_r($create_a_freak.get()); //gets values Yes, it have protected, private and public. But it misses internal. Internal functions and vars are visible only in file. Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1490567 Share on other sites More sharing options...
Jacques1 Posted September 10, 2014 Share Posted September 10, 2014 An internal modifier doesn't make a lot of sense in PHP, because you generally have only one class definition per file. In fact, auto-loaders expect this structure. At best, you might come up with a scenario where you have an auxiliary class which is only supposed to be used by another class in the same file. In that case, the modifier theoretically makes sense. But it would be very confusing and against all common practice. PHP simply isn't C#, and it won't become that in the near future. Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1490611 Share on other sites More sharing options...
deathbeam Posted September 10, 2014 Author Share Posted September 10, 2014 There is also another scenario. Like this: class base{ public function setParam($key, $callable){ $this->params[$key] = $callable; } internal function run(){ foreach($this->params as $key=>$callable) $callable(); } } $base = new Base(); // if internal was possible, users will not be able to call this method outside this file. Especially usefull for frameworks and PHP applications. $base->run(); If you know what I mean. And yes, PHP isn´t C#, but PHP hosting is cheaper than ASP.NET. So it is pity that internal does not exists in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1490627 Share on other sites More sharing options...
CroNiX Posted September 10, 2014 Share Posted September 10, 2014 Isn't what you are calling internal the same as private? Try calling a private method from outside the class. Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1490633 Share on other sites More sharing options...
Jacques1 Posted September 10, 2014 Share Posted September 10, 2014 I don't think this is good code. The general rule is that a file either contains declarations or has side-effects, not both. This is explictly specified in the PSR standards. Of course you're free to violate the rules. For example, many people do have function declarations within regular scripts. But a complete class? That's just spaghetti code and very confusing. Isn't what you are calling internal the same as private? Try calling a private method from outside the class. He wants to restrict properties to one script. So other classes within the same file do have access, but not the classes in other files. Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1490637 Share on other sites More sharing options...
KevinM1 Posted September 10, 2014 Share Posted September 10, 2014 (edited) Deathbeam is actually explaining the 'internal' keyword wrong. C# has what are known as assemblies, which are either executables (actual .exe) or dynamically linked libraries (.dll). These assemblies can be created by/contain multiple files. So, what he wants is a way to say "these files belong to a framework assembly, so most of what's in them should be hidden from the public." 'Internal' says that only code that's in the same assembly can access it. Edited September 10, 2014 by KevinM1 2 Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1490671 Share on other sites More sharing options...
CodeBuilder Posted January 28, 2016 Share Posted January 28, 2016 There is also another scenario. Like this: class base{ public function setParam($key, $callable){ $this->params[$key] = $callable; } internal function run(){ foreach($this->params as $key=>$callable) $callable(); } } $base = new Base(); // if internal was possible, users will not be able to call this method outside this file. Especially usefull for frameworks and PHP applications. $base->run(); If you know what I mean. And yes, PHP isn´t C#, but PHP hosting is cheaper than ASP.NET. So it is pity that internal does not exists in PHP. i think you can use "final" for that... class base{ public function setParam($key, $callable){ $this->params[$key] = $callable; } final function run(){ foreach($this->params as $key=>$callable) $callable(); } } If you use like this then you cannot modify this method in any derived classes.. Quote Link to comment https://forums.phpfreaks.com/topic/290965-php-internal-access-modifier/#findComment-1530516 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.