Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I know there are a lot of information according to this, but I couldn't find any really useful and understandable. So what is the difference between
public static function
and
public function
? Why sometimes I have to use the word
static
?
I know that static functions are called by
::
and non-static by
->
, but the real difference (why sometimes I should use
static
I don't get). As I understand now, when I use
static
, I can call it from another class. Without it - I cannot. But I'm pretty sure that I'm wrong.
I think this information would be really helpful for a lot of ppl. Thanks for your time in trying to explain it.
Static
means that it can be accessed without instantiating a class. This is good for constants.
Static
methods need to have no effect on the state of the object. They can have local variables in addition to the parameters.
Public:
Public
declared items can be accessed everywhere.
Example:
class test {
public function sayHi($hi = "Hi") {
$this->hi = $hi;
return $this->hi;
class test1 {
public static function sayHi($hi) {
$hi = "Hi";
return $hi;
// Test
$mytest = new test();
print $mytest->sayHi('hello'); // returns 'hello'
print test1::sayHi('hello'); // returns 'hello'
More info:
http://php.net/manual/en/language.oop5.visibility.php
–
Keyword static in OOP approach describes functions/variables that are not instantiated with a new object, but are available without instantiation and are shared among all instances of an object.
It can be handy to use, for a variable you can have a "counter" or "flag" shared among objects of the same class.
Or if you are building an utils class library you might want a static singleton with static methods, so any code from a project can call it without the need for "new" call.
Static means that it can be accessed without instantiating a class.
This is good for constants.
Static methods need to have no effect on the state of the object. They
can have local variables in addition to the parameters.
Now why you write public function
The visibility of a property, a method or (as of PHP 7.1.0) a constant
can be defined by prefixing the declaration with the keywords public,
protected or private. Class members declared public can be accessed
everywhere. Members declared protected can be accessed only within the
class itself and by inheriting and parent classes. Members declared as
private may only be accessed by the class that defines the member.
Using public function example
class MyClass
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
echo $this->public;
echo $this->protected;
echo $this->private;
$obj = new MyClass();
Also Static example
class Foo {
public static function aStaticMethod() {
// ...
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
The simple and visible difference between public static function and public function is : Static function you can access without creating object.
Following code snippet give you more clarity :
class my_public {
public function myfunc() {
$val = "Hello I am public function";
return $val;
class my_static_public {
public static function myfunc() {
$val = "Hello I am public static function";
return $val;
// Access Test
$mytest = new my_public();
print $mytest->myfunc(); // returns 'Hello I am public function'
print my_static_public::myfunc(); // returns 'Hello I am public static function'
More about static you click here.
public allows access from everywhere
private allows access only from within the class itself
The keyword static makes a function accessible without the need of an instance of this class. This means you can call static functions from any context, without the need to create an instance of the class, which you would have to do otherwise. Attention: As you don't have an object you can refer to when calling static methods, you can not use the this keyword.