-2

I want to know what is the scope that function who is out of class. Is it private, public or protected ?

function abc {
   //code here
}

class xyz {
 function car () {
   // code here 
 }
}

Now what is the abc function scop ?

Please help me

Harman
  • 1,703
  • 5
  • 22
  • 40

2 Answers2

1

Functions outside any class are global an can be called from anywhere. The same with variables.. just remember to use the global for the variables...

e.g

<?php
function abc() {  }

$foo = 'bar';

class SomeClass {  
 public function tada(){
     global $foo;

     abc();
     echo 'foo and '.$foo;
 }
}
?>

functions are defined at a global level ; so, you don't need to do anything to use them from a method of your class.

For more informations, see the function page in the manual, which states (quoting) :

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

If a "function" is defined inside a class, it's not called a "function" anymore, even if it is still the function that is used : it is called a "method"

Methods can be used statically :

MyClass::myMethod();

Or Dynamically :

$obj = new MyClass();
$obj->myMethod();

Depending on whether they were defined as static or not.

sunny
  • 1,156
  • 8
  • 15
  • what is that public, private or protected ? – Harman Apr 01 '14 at 05:55
  • @user3425009 functions outside any class are global an can be called from anywhere. The same with variables.. just remember to use the global for the variables... – sunny Apr 01 '14 at 06:03
  • @user3425009 I have edited my answer, perhaps this will help you. – sunny Apr 01 '14 at 06:06
  • and obviously the scope is public for every class, because its available for every class, irrespective of hierarchy – sunny Apr 01 '14 at 06:08
-1
function abc {
   //code here
}

This is a public function because function scope mainly in class and in your case abc not inside a class so it behave as a public function

public scope to make that variable/function available from anywhere, other classes and instances of the object.

private scope when you want your variable/function to be visible in its own class only.

protected scope when you want to make your variable/function visible in all classes that extend current class including the parent class.

reference What is the difference between public, private, and protected?

Community
  • 1
  • 1
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55