Using properties and methods without instantiation
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.
How to Use the Static Keyword with Properties
access modifier static property
How to Use the Static Keyword in Methods Accessor static function methods
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?
How to call properties using the static keyword
class name :: property name;
How to call a method with the static keyword
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. ^-^ *