$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>'; echo '<p>' . $newCar->has4WD . '</p>'; echo '<p>' . $newCar->doMoreStuff() . '</p>'; // wouldn't work
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
$this->color = $color; } } class SUV extends Car { public $has4WD = false; // child classes can override parent properties public function getStatus() { // child classes can still access parent methods using parent:: return parent::getStatus(); } } $myCar = new Car('Mercedes', '760GL', 'black'); echo '<p>The color of my car is ' . $myCar->color . '.</p>'; $yourCar = new SUV('Hummer', 'H2', 'red'); echo '<p>The color of your car is ' . $yourCar->color . '.</p>'; echo '<p>My car status is ' . $myCar->getStatus() . '</p>'; $myCar->startEngine(); echo '<p>My car status is ' . $myCar->getStatus() . '</p>'; //$yourCar->startEngine(); echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>'; echo '<p>Your car ' . (empty($yourCar->has4WD) ? 'does not' : 'does') . ' have 4WD.</p>'; echo '<p>My car ' . (empty($myCar->has4WD) ? 'does not' : 'does') . ' have 4WD.</p>'; echo '<p>Say something: ' . Car::saySomething() . '</p>'; // works but not recommended echo $myCar::saySomething(); echo $myCar->saySomething(); ?>
return $this->passKey; } public function getPassKey() { return 'foo'; } } // create an instance of the Car class $myCar = new Car('Mercedes', '760EL', 'black'); output(Car::$instanceCount); // public properties are externally accessible //$myCar->color = 'red'; echo '<p>' . $myCar->color . '</p>'; //$myCar->status = 'on'; // generates an access fatal error_get_last echo $myCar->startEngine(); // works $yourCar = new SUV('Ford', 'Escalade', 'green'); echo '<p>' . $yourCar->model . '</p>'; echo '<p>' . $yourCar->has4WD . '</p>'; $f = 'startEngine'; output($yourCar->{$f}()); echo '<p>' . $yourCar->startEngine() . '</p>'; echo '<p>' . $myCar->getPassKey() . '</p>'; echo '<p>' . $yourCar->getPassKey() . '</p>'; echo '<p>' . $yourCar->getStatus() . '</p>'; echo '<p>' . $yourCar->getKey() . '</p>'; output(Car::$instanceCount); unset($myCar); // destroy object, calling destructor in the process $c = 'Car'; output($c::$instanceCount);