mike
mike

Reputation: 1137

php function namespace

in C , i can define a function to be static so it can only be used in it's own file. in python , i can define a function with it's name starts with _ so this function can't be used outside this finle.

could i do it in php ?

Upvotes: 1

Views: 609

Answers (2)

KingCrunch
KingCrunch

Reputation: 131861

If you really mean "functions": No, both arent possible.

First: Functions are always static.

Second: In PHP namespaces are not bound to a file. So a file can declare non, one or more namespaces. On the other side a namespace can be declared in different files. Its difficult to define a consistent way on how non-public functions can get resolved. You can use static classes instead.

Upvotes: 2

Shakti Singh
Shakti Singh

Reputation: 86346

A class can be used to for data hiding and implementing encapsulation.

You can use private keyword to declare functions in php to hide them outside of code but the all are bounded with class.

Only class can use these type of functions.

class A
{
   /* This method will only be accessible in this 
      class only by the other methods of this class 
     and will be hide from rest of program code*/

  private function setValues()
  {
      //some stuff
  }
  public function getVal()
  {
     $this->setValues();
  }

}

The method above can only be accessible by this class.

Upvotes: 0

Related Questions