Jump to content
deathbeam

PHP "internal" access modifier

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Isn't what you are calling internal the same as private? Try calling a private method from outside the class.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by KevinM1
  • Like 2

Share this post


Link to post
Share on other sites

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..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.