-1

As I have read Static properties cannot be accessed through the object using the arrow operator ->. The static properties can be accessed through the class name with the resolution operator.

In the following example, I am able to access the static method through the object using the arrow operator ->.

class Foo {
   public static $name="I am php";
    public static function aStaticMethod() {
        // ...
        echo 'In Static method';
    }
}

Foo::aStaticMethod();//output: In Static method    
$obj = new Foo;   
$obj->aStaticMethod();//output: In Static method
$obj->name;

output:

In Static methodIn Static method

But when try to access the variable $name through operator -> it gives following error:

PHP Notice:  Accessing static property Foo::$name as non static in /home/jdoodle.php on line 14
PHP Notice:  Undefined property: Foo::$name in /home/jdoodle.php on line 14

Does php really support OOPs properly and what do you mean by Static properties cannot be accessed through the object using the arrow operator ->?

Thanks

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51

2 Answers2

2

The problem is due to the misunderstanding of what a property is, a property is a variable defined in a class. If you expand your example above and add a property...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

class Foo {
    public static $a = "static property";

    public static function aStaticMethod() {
        // ...
        echo 'In Static method';
    }
}

Foo::aStaticMethod();//output: In Static method
echo Foo::$a;
$obj = new Foo;
$obj->aStaticMethod();//output: In Static method
echo $obj->a;

This tries to reference $a, but the output is...

In Static methodstatic propertyIn Static methodPHP Notice:  Accessing static property Foo::$a as non static in /home/nigel/workspace2/Test/t2.php on line 18
PHP Stack trace:
PHP   1. {main}() /home/nigel/workspace2/Test/t2.php:0
PHP Notice:  Undefined property: Foo::$a in /home/nigel/workspace2/Test/t2.php on line 18
PHP Stack trace:
PHP   1. {main}() /home/nigel/workspace2/Test/t2.php:0

Notice: Accessing static property Foo::$a as non-static in /home/nigel/workspace2/Test/t2.php on line 18

Call Stack:
    0.0001     348024   1. {main}() /home/nigel/workspace2/Test/t2.php:0


Notice: Undefined property: Foo::$a in /home/nigel/workspace2/Test/t2.php on line 18

Call Stack:
    0.0001     348024   1. {main}() /home/nigel/workspace2/Test/t2.php:0

(Note that I've set it to report all errors/warnings etc.)

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
-1

As per PHP doc:

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

Example from PHP doc

dpattayath
  • 160
  • 5
  • I am also reading this article that why I asked this question http://php.net/manual/en/language.oop5.static.php – Gufran Hasan Jun 27 '18 at 06:26
  • uh! right, sorry, I think in PHP terms, static methods have the benefit of being called from a class context alongside from object context not an enforcement. Commenting the second part of your question, in my opinion, PHP supports OOPS paradigm programming, but not a 10/10 OOPS language. Because "print((new Foo())::$name)" will work in PHP which doesn't make sense as am chaining an object end of the day not a class. – dpattayath Jun 27 '18 at 06:35