PHP Classes and Objects Examples
Posted on March 22, 2018 in PHP by Matt Jennings
Class Example, Object, and Access Modifiers (Public, Private, Protected) Examples
/* public - the property or method can be accessed from everywhere. This is default protected - the property or method can be accessed within the class and by classes derived from that class private - the property or method can ONLY be accessed within the class */ class Person { const AVG_LIFE_SPAN = 79; public $firstName; public $lastName; public $yearBorn; // Constructor function that is called // whenever an object is instantiated // from the class function __construct() { $this->firstName = 'Samuel Longhorne'; $this->lastName = 'Clemens'; $this->yearBorn = 1899; } public function getFirstName() { return $this->firstName . ' ' . $this->getLastName(); } private function getLastName() { return $this->lastName ; } protected function kewlPerson() { return getFirstName() . ' is a kewl person!'; } static public function blah1() { return 'blah1'; } static protected function blah2() { return 'blah2'; } } class DeadPerson extends Person { public function getDeadPersonInfo() { // Grabbed $this->firstName and other variables // from Person class because they're inherited return $this->firstName . ' ' . $this->lastName . ', ' . $this->yearBorn . ' is now dead!'; } } class OtherPerson { public $randomPersonName; public function __construct($randomPersonName) { $this->randomPersonName = $randomPersonName . ': ' . Person::blah1(); } public function getRandomPerson() { return $this->randomPersonName; } public function protectedTest() { return Person::blah2(); } } // Class instantiation or object creation $personObj = new Person(); /* Public method output, including the private method function call inside the public method: 'Samuel Longhorne Clemens' */ echo '$personObj: ' . $personObj->getFirstName() . PHP_EOL; // Class instantiation or object creation // on inherited object $deadPersonObj = new DeadPerson(); // Inherited class method call echo '$deadPersonObj: ' . $deadPersonObj->getDeadPersonInfo() . PHP_EOL; // Another class instantiation, or object creation // and Person::blah1() is echo out at the end because it's // accessing a static class and using the double colon operator $otherPersonObject = new OtherPerson('Billie'); echo $otherPersonObject->getRandomPerson() . PHP_EOL; /* Method call below doesn't work because Person::blah2() method was a static class */ echo $otherPersonObject->blah2();
Access Modifiers
<?php /* public - the property or method can be accessed from everywhere. This is default protected - the property or method can be accessed within the class and by classes derived from that class private - the property or method can ONLY be accessed within the class */ class Fruit { public $name; public $color; public $weight; function set_name($n) { // a public function (default) $this->name = $n; } protected function set_color($n) { // a protected function $this->color = $n; } private function set_weight($n) { // a private function $this->weight = $n; } } $mango = new Fruit(); $mango->set_name('Mango'); // OK $mango->set_color('Yellow'); // ERROR $mango->set_weight('300'); // ERROR ?>