1

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 ?

1
  • functions != methods. Do you really mean "functions"? What is a "finle"? Commented Apr 14, 2011 at 5:56

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

you say "both aren't possible" ,did you mean in c and python ? how ? and you say : Functions are always static , in c? in python , or just in php ? i'm confused by what you say .
@mike: I dont know c and python well enough, but the question is about php, isnt? -- In PHP functions (!= methods) dont have a state (like methods in objects (properties)), so they can be treated as "always static". They are callable from everywhere (non-static methods require a object-context) and are not effected by anything else than the given arguments (thats not true at all, e.g. if it uses a time function or relies on the global state (ugly ;)).
0

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.

2 Comments

Not quite the same as file scope though.
@deceze: I know but there is no other way to do this except class. I don't think there is something wrong if I explained using class instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.