0

I have below code

class MyClass
{
    public static $count = 0;

    public static function plusOne()
    {
        return "The count is " . ++self::$count . ".<br />";
    }
}

$obj = new MyClass;
$obj->plusOne(); //Works proper for static function

echo '<br/>'.MyClass::$count; // count: 1

MyClass::plusOne(); //Works proper for static function
echo '<br/>'.MyClass::$count; // count: 2

----OUTPUT------
1
2

----------------
  1. I have access static method by creating object (using ->) and by scope resolution operator (using ::) Method works properly But when I am trying to use static Property by scope resolution (using :: ) It display notice don't work. below is additional code.

    $obj->count; // Notice: Undefined property: MyClass::$count

  2. I have access static method by creating object (using ->) and by scope resolution operator (using ::) BUT still static properties count increases WHY?

  3. Why Different access rule for static property (only :: ) and method (by ->, ::)?

Eduard Uta
  • 2,477
  • 5
  • 26
  • 36
Sandeep
  • 956
  • 2
  • 9
  • 19
  • You can use `->` when you've instantiate a class as an object. `::` is when you want to call something by `static`. You can not access `->$count` because that is a class member, and you can access by `::` when you did not instantiate. – vaso123 Jan 13 '15 at 13:30
  • possible duplicate of [Call static method from instance in PHP, future deprecation?](http://stackoverflow.com/questions/6398658/call-static-method-from-instance-in-php-future-deprecation) – Maxim Krizhanovsky Jan 13 '15 at 13:59
  • @lolka_bolka: Thanks. In Q2 I have used method 2 different(i.e. ->, :: ) but count increased at the end. first time i have used $obj->plusOne() and second time directly MyClass::plusOne() any idea? – Sandeep Jan 13 '15 at 14:34
  • @Darhazer: Thanks. I got my Q1 answer through your link. Could you please help me to resolve Q2 How/Why its happen? – Sandeep Jan 13 '15 at 14:37
  • as @lolka_bolka pointed out, using designation `->` does not mean it **has** to be a non-static method, it just has to be a method defined in the class from which an object was instantiated. – dbf Jan 13 '15 at 21:37

1 Answers1

-1

In your example it looks odd, but imagine if you also had

class MyOtherClass
{
    public $count = 0;

    public function plusOne()
    {
        return "The other count is " . ++$this->count . ".<br />";
    }
}

Now if you want to do

$obj->plusOne();

You can do that and it will work regardless of if $obj is MyClass or MyOtherClass. The implementation details aren't important at this point, unless you already know them - eg. you can use MyClass::plusOne() without instantiating it, because you know that's a static method.

Static variables are always static as declared. There is no data on the instance - so whatever scope you're using when you update it, it will update the static count.

James Waddington
  • 2,894
  • 2
  • 15
  • 24
  • Thanks But I need to know why difference in accessing static variable and static function not alternative code. – Sandeep Jan 13 '15 at 13:49
  • Sure, the code just intended to illustrate the point: when you access a method on a variable, that variable might not always be the same class. It might have a class where that method is static, or it might have one where it is not. Using -> you can invoke the given method in either case. – James Waddington Jan 13 '15 at 13:57