#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    What is the difference between $object->show_name(); and student::show_name();


    They both print the same thing.
    PHP Code:
    class student{
        public function 
    show_name(){
            echo 
    'Test';
        }
    }
    echo 
    student::show_name();
    #vs
    $object = new student();
    echo 
    $object->show_name(); 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    12
    Hi,

    they only print the same thing if you have all error messages turned off (which isn't a good idea for developing). When you turn them on again, the first variant will spill out a warning:

    Code:
    Strict Standards: Non-static method student::show_name() should not be called statically in C:\xampp\htdocs\test.php on line 9
    But this message is a gross understatement. This is not about adhering to some coding style standards. Calling a non-static method in a static way (with the class as the receiver) is simply wrong and will lead to severe errors when you try to refer to $this within the method. A real programming language like Java would abort with a fatal error saying something like

    Code:
    Called non-static method in static context
    So only the second variant is correct. The first one completely relies on PHP's sloppiness.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by Jacques3
    Hi,

    they only print the same thing if you have all error messages turned off (which isn't a good idea for developing). When you turn them on again, the first variant will spill out a warning:

    Code:
    Strict Standards: Non-static method student::show_name() should not be called statically in C:\xampp\htdocs\test.php on line 9
    But this message is a gross understatement. This is not about adhering to some coding style standards. Calling a non-static method in a static way (with the class as the receiver) is simply wrong and will lead to severe errors when you try to refer to $this within the method. A real programming language like Java would abort with a fatal error saying something like

    Code:
    Called non-static method in static context
    So only the second variant is correct. The first one completely relies on PHP's sloppiness.
    I'm testing it in Zend Studios Run CLI application.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    12
    So? Does that mean that error_reporting(-1) has no effect? Then I'm pretty sure there's another way to get the error messages (which should be explained in the documentation).
  8. #5
  9. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,094
    Rep Power
    1990
    Originally Posted by zxcvbnm
    I'm testing it in Zend Studios Run CLI application.
    How it's run doesn't matter at all. I mean that, not at all.

    You should never call a non-static method in a static way. I repeat never. Jacques explained why, and he is 100% correct.

    Use this as an example in your code...
    PHP Code:
    class student
        public 
    $name;
        public function 
    show_name(){ 
            echo 
    $this->name;
        } 
    }

    student::show_name(); 
    You will get an error no matter how your running it because this is the wrong way to do it. I can't remember if this is a fatal error or not... because I won't try to do things this way (as it's the wrong way to do it).
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    If you call a non-static member in static context and you're already deep within another object, even WEIRDER things happen. Check this out:

    PHP Code:
    <?php
    class {
      public 
    $foo 'bar CLASS A';
      
      public function 
    doIt() {
        
    b::getFoo();
      }
    }

    class 
    {
      public 
    $foo 'bar CLASS B';
      public function 
    getFoo() {
        echo 
    $this->foo;
        echo 
    "\n";
        echo 
    get_class();
        echo 
    "\n";
        echo 
    get_class($this);
        echo 
    "\n";
      }
    }

    $a = new a();
    $a->doIt();
    Prints:

    Code:
    bar CLASS A
    b
    a
    The explanation of WHY isn't really important (PHP internally falls back to the last valid $this) but if you call a non-static method statically, $this could be pointing to a completely unrelated object and you'll never figure it out. Only call a method statically if it was designed to be static and is marked with the static keyword. Adding the 'static' keyword to the code above causes it to die the way it should.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo