0

Referring to http://php.net/manual/en/language.oop5.static.php,

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

Why does the example instantiate the class ($foo = new Foo();) before print $foo::$my_static? As per the above statement only

  print Foo::$my_static

OR

  $classname = 'Foo';
  print $classname::$my_static 

is correct.

example1.php

     public function staticValue() {
     return self::$my_static;
     }
 }

  class Bar extends Foo
 {
    public function fooStatic() {
    return parent::$my_static;
  }
}


 print Foo::$my_static . "\n";

 $foo = new Foo();
 print $foo::$my_static . "\n";
 $classname = 'Foo';
 print $classname::$my_static . "\n"; // As of PHP 5.3.0

     ?> 

example2.php

<?php
   class Foo{
        static $myVar="foo";
        public static function aStaticMethod(){
    return self::$myVar;
}
  }

 $foo=new Foo;
 print $foo->aStaticMethod();
?>

The above example doesn't give any error. Is it a good practise to access a static method with an instantiated class object?

thank you.

0

2 Answers 2

3

I think the description you quote is slightly unclear/ambiguous. They refer to $foo->my_static being not possible. This is later repeated in this statement:

Static properties cannot be accessed through the object using the arrow operator ->.

$foo::$my_static is possible though. The object instance just stands in for the class name, it doesn't really change how the static property is used and is mostly a convenience shortcut.

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

4 Comments

Maybe someone should file a bug report against the documentation here.
I'm totally confused with the php.net statement. Can I just stick to Foo::$my_static since "Static properties and methods are accessible without needing instantiation of the class"? Or if the stmt is incorrect, shall I stick to $foo=new Foo (instance of the class); $foo::$my_static? Which is the best?
@vaan You don't need to instantiate an object to access the classes' static properties. Just use Foo::$my_static. Only if you already happen to have an object, you could use $foo::$my_static as a (sometimes) convenient shortcut.
Ok, thanks a lot. I will stick to your suggestion deceze. What about static methods, is it good practise to access static method with instantiated class object as in example2 of my question?
1

In almost all OO programming languages, you can access static members via an instance of the class. C++ allows this, Java allows this (although it gives a warning).

The reason for accessing statics through the class name and not through an instance of the class is mainly due to readability, which is why I suggest you do the same.

Comments

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.