1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-07-12 11:06:20 +02:00
Files
phpapprentice/code/static.php
2018-09-05 20:18:32 -05:00

71 lines
2.0 KiB
PHP

<?php
// When writing a class, all of the properties and methods are being defined for the object
// that will be created from the class.
class House
{
public $color;
public function __construct($color)
{
$this->color = $color;
}
}
// Like building a house, a class is a blueprint that
// defines what the house can do and the object is the house itself that can actually
// perform the actions defined in the blueprint.
$house = new House('Green');
// However, what if you want the blueprint to have properties and methods?
// That is when you use the "static" keyword. In this class, we will define a default color
// on the class itself and then use it when creating a new object.
class Skyscraper
{
private static $popularColor;
public $color;
public static function setDefaultColor($color)
{
self::$popularColor = $color;
}
public function __construct()
{
$this->color = self::$popularColor;
}
}
// You can access static methods and properties using double colons on "self" inside the object
// or on the class name outside of the object. Static methods and properties can only access
// other static methods and properties.
Skyscraper::setDefaultColor('Grey');
$skyscraper = new Skyscraper();
echo $skyscraper->color . "\n";
// Often, you will see static constructors in PHP.
// A static constructor creates a new instance of an object. Why would do that when you just use "new Class" to create
// a new object? The most common reason is to make the code more readable.
class TinyHouse
{
private $color;
private $wheels;
private $trailer;
public static function build($color, $wheels, $trailer)
{
return new self($color, $wheels, $trailer);
}
public function __construct($color, $wheels, $trailer)
{
$this->color = $color;
$this->wheels = $wheels;
$this->trailer = $trailer;
}
}
// Using "build" can make more sense than "new", but it is ultimately a personal choice.
$house = TinyHouse::build('Blue', 4, true);