4

I was reading a book about PHP when I ran into a strange part of code:

 class Employee {
        public function show() {
            echo "show launched\n";
        }
    }

    Employee::show();

I came from C++ so I was going to bet this code wouldn't work. This is why I tested it.

And it worked, showing "show launched" (omg, am I drunk?)!

It seems to be breaking the concept that method of class can be called without instantiation of class.

  • What is the point then of static identifier in classes?
  • Are all public functions static too? Really, what am I missing?

Thanks in advance.


Addition: Just a notice.

I found that in this book. Pages 178-179 and it's given as correct example (if I'm right)

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Tebe
  • 3,176
  • 8
  • 40
  • 60

4 Answers4

11

Yeah that would work but with a warning. You may have turned off your error reporting on PHP by the way...

Strict standards: Non-static method Employee::show() should not be called statically

Adding a static keyword before the function definition would make the warning dissappear.

Below code works without a warning..

<?php
class Employee {
    public static function show() { //<----- Added the static keyword.
        echo "show launched\n";
    }
}

Employee::show();

To answer your question...

It seems to be breaking the concept that method of class can be called without instantiation of class.

Yeah that is correct, that's why you are getting a pretty clear cut warning as I showed you earlier. You know what a warning does right ? ;). Something that should not be done.

From the PHP Docs..

Calling non-static methods statically generates an E_STRICT level warning.

Source

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

It has different behavior related on php version. PHP 4 did not have a static keyword (in function declaration context) but still allowed methods to be called statically with ::. This continued in PHP 5 for backwards compatibility purposes.

However with the changes in the object model with PHP 5 - the static keyword has been introduced. And then since PHP 5.1.3 you get proper strict standard warnings about those like:

Strict Standards:  Non-static method Employee::show() should not be called statically in ...
Nick Bondarenko
  • 6,211
  • 4
  • 35
  • 56
1

I think it depends on php version you are using. This feature is deprecated in the new php version and will be removed in the future versions.

this will not work and you will get error when using the latest php versions.

I am using PHP 5.5.9-1+sury.org~precise+1 (cli) (built: Feb 13 2014 15:53:53)

and if turn errors on and put your code as it is.

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

 class Employee {
        public function show() {
            echo "show launched\n";
        }
    }

    Employee::show();

?>

This is error message I am getting.

**Strict Standards: Non-static method Employee::show() should not be called statically in /var/www/test/index.php on line 19
show launched**
Mubo
  • 1,078
  • 8
  • 16
0

The Code Works you will get a Warning

Strict standards: Non-static method Employee::show() should not be called statically

Just add the static keyword in the function like public static function show()

Nambi
  • 11,944
  • 3
  • 37
  • 49