This is a basic PHP program based on object-oriented programming which explains how to use the public access modifier in PHP with an example and output.
The public modifier is called with the Public
keyword before the defined class name. If you have added the public before any class, then that class will be accessible from outside of class.
After this tutorial, your basic concept of Object-oriented programming will be clear.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php class Car { //public methods and properties. public $model; public function getModel() { return "The car model is " . $this -> model; } } $mercedes = new Car(); //Here we access a property from outside the class $mercedes -> model = "Mercedes."; //Here we access a method from outside the class echo $mercedes -> getModel(); ?> |
The car model is Mercedes.
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.