PHP born as a very simple Computer Language to help building Dynamically Generated Server Side Html pages to be rendered in the Computer using a Web Browser. Since then, new features, new extensions, new statements, new libraries, etc. has been added to the core executable.
Currently it completely supports Object Oriented Programming and it has an outstanding Exception handling.
Prerequisites:
In PHP we create new objects instantiating a Class:
$zebra = new Animal('zebra');
in here we are assigning to the variable $zebra a new Animal Class and we are setting the species property passing 'zebra' as string parameter to the Class Constructor.
Once we have an object instance, we can access its properties and methods using either -> (Arrow Function Call) or :: (Scope Resolution Operator).
There are a few rules that apply to accessing properties and methods in PHP:
:: can only be used if the method is declared Static.
<?php
namespace App\Entity;
class Zoo
{
/**
* @var string|null
*/
private ?string $zooName;
/**
* @param string|null $zooName
*/
public function __construct(?string $zooName)
{
$this->zooName = $zooName;
}
/**
* This method can be called using ::
* @param string $a
* @param string $b
* @return string
*/
public static function coolZoo(string $a, string $b): string
{
return 'This Cool Zoo is for ' . $a . ' and ' . $b;
}
}
Now from other file you can make a static function call like this:
$zoo = new Zoo('N.Y. City Zoo');
echo "Sample use of Static Method: <strong>";
echo $zoo::coolZoo('Girls', 'Boys');
echo "</strong>";
and in the Browser you see this:
:: can be used to call a static method in a Class without need to instantiate an object.
echo "Sample use of Static Method: <strong>";
echo Zoo::coolZoo('Girls', 'Boys');
echo "</strong>";
Properties and Methods can only be accessed if declared public.
<?php
namespace App\Entity;
class Zoo
{
/**
* @var string|null
*/
private ?string $zooName;
/**
* @param string|null $zooName
*/
public function __construct(?string $zooName)
{
$this->zooName = $zooName;
}
/**
* @param string $a
* @param string $b
* @return string
*/
public function coolZoo(string $a, string $b): string
{
$welcome = 'Welcome to ' . $this->zooName . '. ';
return $welcome . $this->doZooStuff($a, $b);
}
/**
* @param string $a
* @param string $b
* @return string
*/
private function doZooStuff(string $a, string $b): string
{
return 'This Cool Zoo is for ' . $a . ' and ' . $b;
}
}
From other file you can do:
$zoo = new Zoo('N.Y. City Zoo');
//This works
echo $zoo->coolZoo('Girls', 'Boys');
echo "<br/>";
//This wont work and throws error
echo $zoo->doZooStuff('Girls', 'Boys');
Welcome to N.Y. City Zoo. This Cool Zoo is for Girls and Boys
Fatal error: Uncaught Error: Call to private method App\Entity\Zoo::doZooStuff() from global scope in /var/www/cool_project/php_code/src/index.php:19 Stack trace: #0 {main} thrown in /var/www/cool_project/php_code/src/index.php on line 19
Properties and Methods can only be accessed from one Class to another if declared protected and calling Class is extending the other.
class Animal extends Zoo
{
/**
* @var string|null
*/
private ?string $species;
/**
* @var int|null
*/
private ?int $eyes;
/**
* @var int|null
*/
private ?int $legs;
/**
* @var float|null
*/
private ?float $tail;
/**
* @param string|null $species
*/
public function __construct(?string $zooName, ?string $species)
{
parent::__construct($zooName);
$this->species = $species;
}
/**
* @return string|null
*/
public function getAnimalZoo()
{
return $this->zooName;
}
From other file you can do:
//Zoo Animals
$zebra = new Animal('S.D. Zoo','zebra');
echo 'This ' . $zebra->getSpecies() . ' lives in ' . $zebra->getAnimalZoo();
and in the Browser:
This zebra lives in S.D. Zoo
Pseudo variable $this
is used to refer to the object itself from within an object method.
Private Properties and Methods are accessible only from other Method in the same object using $this
.
/**
* @param string $a
* @param string $b
* @return string
*/
public function coolZoo(string $a, string $b): string
{
$welcome = 'Welcome to ' . $this->zooName . '. ';
return $welcome . $this->doZooStuff($a, $b);
}
/**
* @param string $a
* @param string $b
* @return string
*/
private function doZooStuff(string $a, string $b): string
{
return 'This Cool Zoo is for ' . $a . ' and ' . $b;
}
self
can be used to refer to the object itself and use :: to call properties and methods declared as static and magic methods like __construct
class Zoo
{
/**
* @var string|null
*/
protected ?string $zooName;
static ?string $zooHours = '9am to 5pm Mon to Fri';
class Animal extends Zoo
{
/**
* @var string|null
*/
private ?string $species;
/**
* @var int|null
*/
private ?int $eyes;
/**
* @var int|null
*/
private ?int $legs;
/**
* @var float|null
*/
private ?float $tail;
/**
* @param string|null $species
*/
public function __construct(?string $zooName, ?string $species)
{
parent::__construct($zooName);
$this->species = $species;
}
/**
* @return string|null
*/
public function getAnimalZoo()
{
$zooHours = self::$zooHours;
return $this->zooName . 'open from / to :' . $zooHours;
}
From other file you can do:
//Zoo Animals
$zebra = new Animal('S.D. Zoo','zebra');
echo 'This ' . $zebra->getSpecies() . ' lives in ' . $zebra->getAnimalZoo();
And you get in the Browser:
This zebra lives in S.D. Zooopen from / to :9am to 5pm Mon to Fri
Mastering PHP Object Oriented Programming requires a complete understanding of all these basic rules and principles.
Refer to PHP Classes and Objects for more on this.