Jump to content

PHP "internal" access modifier


deathbeam

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?

Link to comment
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
Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

  • 1 year later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.