} public function getStatus() { $this->doStuff(); // can call the protected function return $this->status; } function __construct($c = 'blue') { $this->color = $c; } } // create an instance of Car class $myCar = new Car(); echo '<p>' . $myCar->color . '</p>'; echo '<p>' . $myCar->getStatus() . '</p>'; echo '<p>' . $myCar->doMoreStuff() . '</p>'; // works echo '<p>' . $myCar->flag . '</p>'; // doesn't work echo '<p>' . $myCar::$flag . '</p>'; // works echo '<p>' . Car::$flag . '</p>'; // must reference the class::property class SUV extends Car { public $has4WD = 'yes'; } $newCar = new SUV('black'); echo '<p>' . $newCar->color . '</p>'; echo '<p>' . $newCar->getStatus() . '</p>';
public function getStatus() { // child classes can still access parent methods using parent:: return parent::getStatus(); }
var $model = ''; protected $status = 'off'; function startEngine() { $this->status = 'running'; // if engine were an object inside the Car object //$this->engine->status = 'running'; } function getStatus() { return $this->status; } } $myCar = new Car(); $myCar->color = 'black'; $myCar->make = 'Mercedes'; $myCar->model = '725L'; echo "<p>My car will be a {$myCar->color} {$myCar->make} {$myCar->model}.</p>"; //$myCar->status = 'running'; // doesn't work because $status is protected echo '<p>Current car status: ' . $myCar->getStatus() . '.</p>'; $myCar->startEngine(); // start 'er up echo '<p>Current car status: ' . $myCar->getStatus() . '.</p>'; $yourCar = new Car(); echo '<p>Your car\'s status: ' . $yourCar->getStatus() . '.</p>'; // Accessing sub-objects might look something like this: //$myCar->engine->start(); ?> </body> </html>
static $doesHave4WD = true; public function getStatus2() { return $this->status; } // static members are accessed directly from the class using :: // they are not accessible from class instances static function has4WD() { // can't use $this in a static method because it's not associated // with an instance //return $this->has4WD; // we also can't access non-static members directly from the class //return SUV::has4WD; // note that the static variable has the $ in the name return SUV::$doesHave4WD; } } $yourCar = new Car(array('make' => 'Cadillac', 'model' => 'El Dorado', 'color' => 'pearl')); $myCar = new SUV(); echo '<p>Your car color is ' . $yourCar->color . '</p>'; echo '<p>Your car ' . (isset($yourCar->has4WD) && $yourCar->has4WD ? 'has' : 'does not have') . ' four-wheel drive.</p>'; echo '<p>My car ' . (isset($myCar->has4WD) && $myCar->has4WD ? 'has' : 'does not have') . ' four-wheel drive.</p>'; echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>'; echo '<p>My car status is ' . $myCar->getStatus() . '</p>'; // generates a warning because $status is private echo '<p>My car status is ' . $myCar->getStatus2() . '</p>'; $myCar->has4WD(); // can't access static properties echo SUV::has4WD(); // access the static member directly from the class
class Car { // variables inside a class are called properties or attributes public $make = null; public $model = null; public $color = null; protected $status = 'off'; // functions inside a class are called methods public function setStatus($value) { // '$this' refers to the current instance $this->status = $value; } public function getStatus() { return $this->status; } } // we create instances (actual objects) of a class using 'new' $yourCar = new Car(); $myCar = new Car(); // use the '->' operator to access an object's members $yourCar->color = 'red'; $yourCar->color = 'blue'; echo '<p>Your car color is ' . $yourCar->color . '.</p>'; //echo $yourCar->status; echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>'; $yourCar->setStatus('running'); echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>'; echo '<p>My car status is ' . $myCar->getStatus() . '</p>';