function fuelConsumption($distance) { $result = $this->fuel * $distance / 100; return $result; } } class Audi extends Car { } $distance = 1000; // $car1 = new Car(); $car1->speed = 50; $car1->fuel = 20; echo "Время в пути " . $car1->rtipTime($distance) . "<br>"; echo "Расход топлива " . $car1->fuelConsumption($distance) . "<br>"; // $car2 = new Car(); $car2->speed = 75; $car2->fuel = 30; echo "Время в пути " . $car2->rtipTime($distance) . "<br>"; echo "Расход топлива " . $car1->fuelConsumption($distance) . "<br>"; // $bic = new Bicycle(); $bic->speed = 50; echo "Время в пути " . $bic->rtipTime($distance) . "<br>"; echo "Расход калорий " . $bic->caloriesBurned($distance) . "<br>"; ?> </body> </html>
echo paint("bedroom", null); echo paint("bedroom"); echo paint("blue"); //You have to pay attention to the order of the paramters you're passing! //A simple inheritance example, where the parent class' method "honk" is accessed using two different syntaxes. class Vehicle { public function honk() { return "HONK HONK!"; } } class Bicycle extends Vehicle { } $bicycle = new Bicycle(); echo $bicycle->honk() . "<br>"; echo Bicycle::honk() . "<br>"; //The :: is a "scope resolution operator". However, according to strict standards, one should not call a non-static method statically. //Accessing a parent class constant const (notice no $ before the constant's name) without creating an object class Hi { const hi = "hello"; } class HelloWorld extends Hi { } echo HelloWorld::hi; //The :: is a "scope resolution operator" echo "<br>"; echo "<br>";
<html> <head> <title>Sobreposição!</title> </head> <body> <p> <?php class Vehicle { public function honk() { return "HONK HONK!"; } } // Adicione seu código abaixo! class Bicycle extends Vehicle { public function honk() { return "Beep beep!"; } } $bicycle = new Bicycle(); echo $bicycle->honk(); ?> </p> </body> </html>
class Car extends Vehicle{ public $gas; function __construct() { parent::__construct("car",4); $this->gas = 100; } function drive(){ parent::drive(); $this->gas-= 5; echo "Now I have $this->gas percent of gas left.<br>";} } $bike = new Bicycle("bell"); echo $bike->drive("bell"); //prints 'Riding a bike with 2 wheels' echo '<br>'; $car = new Car(); echo $car->drive();//prints 'Driving a car with 4 wheels' ?> </p> <?php /* * $_SESSION $_POST $_GET
} ?> <h2>Inheritance</h2> <?php $square = new Square(); if (property_exists($square, "hasSides")) { echo "I have sides"; } ?> <h2>Overriding Parent Methods</h2> <?php $vehicle = new Vehicle(); echo $vehicle->honk() . "<br/>"; $bicycle = new Bicycle(); echo $bicycle->honk() . "<br/>"; echo $bicycle->drive() . "<br/>"; ?> <h2>Using <code>const</code></h2> <?php // :: => scope resolution operator if (Immortal::alive) { echo "I live forever!"; } ?> <h2><code>static</code> Keyword</h2> <?php echo Person::greet() . "<br/>"; echo Person::$shout . "<br/>";
<?php class Model { protected static function validateArgs($args) { throw new Exception("You need to override this in a subclass!"); } public static function find($args) { static::validateArgs($args); $class = get_called_class(); // now you can do a database query, such as: // SELECT * FROM $class WHERE ... } } class Bicycle extends Model { protected static function validateArgs($args) { return true; } } Bicycle::find(['owner' => 'peewee']);