To use the properties and methods in the class, you had to create an instance.
But there are ways to use it without doing so.
Use the keyword static.
- access modifier static property
- access modifier static function method
So let's do it.
- <?php
- class Car
- {
- public static $hello = "Hello? I'm a static property";
- public static function hello()
- {
- return "Hello? I'm a static method";
- }
- }
- ?>
You can declare properties and methods using static as above.
Then shall we use it now?
- class name :: property name;
- class name :: method name;
Let's check it through an example.
- <?php
- class Car
- {
- public static $hello = "Hello? I'm a static property";
- public static function hello()
- {
- return "Hello? I'm a static method";
- }
- }
- echo 'property : '.Car::$hello;
- echo '<br>';
- echo 'method : '.Car::hello();
- ?>
Result
Of course, access modifier should use public. Because it's called outside the class. ^-^ *