class Car {
// The code
}
class Car {
public $comp;
public $color = 'beige';
public $hasSunRoof = true;
}
$bmw = new Car();
class Car {
public $comp;
public $color = 'beige';
public $hasSunRoof = true;
}
$bmw = new Car();
echo $bmw->color; //获取属性 beige
$bmw->color = 'blue'; //设置属性
echo $bmw->color; //获取属性 blue
class Car {
public $comp;
public $color = 'beige';
public $hasSunRoof = true;
public function hello()
{
return "beep";
}
}
$bmw = new Car ();
$mercedes = new Car ();
echo $bmw -> hello(); // beep
echo $mercedes -> hello(); // beep
class Car { // The properties
public $comp;
public $color = 'beige';
public $hasSunRoof = true;
// 这个方法现在可以获取到类内的属性了。
public function hello(){
return "Beep I am a <i>" . $this->comp . "</i>, and I am <i>" . $this->color;
}
}
$bmw = new Car();
$mercedes = new Car ();
$bmw->comp = "BMW";
$bmw->color = "blue";
$mercedes->comp = "Mercedes Benz";
$mercedes->color = "green";
//我们调用hello方法。
echo $bmw->hello(); // Beep I am a BMW, and I am blue.
echo $mercedes->hello(); // Beep I am a Mercedes Benz, and I am green.
class Car {
public $tank; //剩余的油量
//加油
public function fill($float) {
$this-> tank += $float;
return $this;
}
// 汽车行驶并且消耗油
public function ride($float) {
$miles = $float;
$gallons = $miles/50;
$this->tank -= ($gallons);
return $this;
}
}
$bmw = new Car();
//加10份油,并且行驶40miles,得到最后的剩余油量$tank.
$tank = $bmw->fill(10)->ride(40)->tank;
echo $tank;
//result 9.2
class Car {
private $model;
public function getModel()
{
return "The car model is " . $this -> model;
}
}
$mercedes = new Car();
//无法从类的外部访问或者设置model私有属性的值。故会报错
$mercedes->model="Mercedes benz";
echo $mercedes->getModel();
<?php
class Car {
private $model;
//public的方法允许被类外部代码访问
public function setModel($model)
{
$this->model = $model;
}
public function getModel()
{
return "The car model is " . $this->model;
}
}
$mercedes = new Car();
$mercedes -> setModel("Mercedes benz");
echo $mercedes -> getModel();
?>
class Car{
private $model;
// A constructor method.
public function __construct($model)
{
$this -> model = $model;
}
}
$car1 = new Car ('mercedes');
class Car {
private $model = '';
//__construct
public function __construct($model = null)
{
if($model)
{
$this -> model = $model;
}
}
public function getCarModel()
{
return " The <b>" . __CLASS__ . "</b> model is: " . $this -> model;
}
}
$car1 = new Car('Mercedes');
echo $car1 -> getCarModel();
// The
// Car
// model is: Mercedes
__LINE__ | 得到使用该常数的行号 |
__FILE__ | 获取使用该常量的完整路径或文件名。 |
__METHOD__ | 获取使用该常量的方法的名称 |
class Parent {
// The parent’s class code
}
class Child extends Parent {
// The child can use the parent's class code
}
//父类
class Car {
private $model;
//Public setter method
public function setModel($model)
{
$this -> model = $model;
}
public function hello()
{
return "beep! I am a <i>" . $this -> model . "</i><br />";
}
}
//子类
class SportsCar extends Car {
}
//实例化一个子类
$sportsCar1 = new SportsCar();
//使用父类的方法
$sportsCar1 -> setModel('Mercedes Benz');
echo $sportsCar1 -> hello();
// The parent class has hello method that returns "beep".
class Car {
public function hello()
{
return "beep";
}
}
//The child class has hello method that returns "Halllo"
class SportsCar extends Car {
public function hello()
{
return "Hallo";
}
}
$sportsCar1 = new SportsCar();
echo $sportsCar1 -> hello(); //Hallo
// The parent class has hello method that returns "beep".
class Car {
final public function hello()
{
return "beep";
}
}
class SportsCar extends Car {
public function hello()
{
return "Hallo";
}
}
$sportsCar1 = new SportsCar();
echo $sportsCar1 -> hello();
abstract class Car {
abstract public function calcNumMilesOnFullTank();
}
abstract class Car {
protected $tankVolume;
public function setTankVolume($volume)
{
$this -> tankVolume = $volume;
}
// Abstract method
abstract public function calcNumMilesOnFullTank();
}
class Honda extends Car {
//因为我们继承了抽象类,所以我们需要在子类中定义它的抽象方法,
//并且向方法的主体添加代码
public function calcNumMilesOnFullTank()
{
$miles = $this -> tankVolume*30;
return $miles;
}
}
interface Car {
public function setModel($name);
public function getModel();
}
class miniCar implements Car {
private $model;
public function setModel($name)
{
$this -> model = $name;
}
public function getModel()
{
return $this -> model;
}
}
interface Vehicle {
public function setHasWheels($bool);
public function getHasWheels();
}
class miniCar implements Car, Vehicle {
private $model;
private $hasWheels;
public function setModel($name)
{
$this -> model = $name;
}
public function getModel()
{
return $this -> model;
}
public function setHasWheels($bool)
{
$this -> hasWheels = $bool;
}
public function getHasWheels()
{
return ($this -> hasWheels)? "has wheels" : "no wheels";
}
}
interface | abstract class | |
代码 |
|
|
可见性 |
|
|
同一个类可以同时实现多个接口 | 同一个类只能继承一个抽象类 |
class car {
protected $model;
protected $hasSunRoof;
protected $numberOfDoors;
protected $price;
// string type hinting
public function setModel(string $model)
{
$this->model = $model;
}
// boolean type hinting
public function setHasSunRoof(bool $value)
{
$this->hasSunRoof = $value;
}
// integer type hinting
public function setNumberOfDoors(int $value)
{
$this->numberOfDoors = $value;
}
// float type hinting
public function setPrice(float $value)
{
$this->price = $value;
}
}
class Car {
protected $driver;
// The constructor can only get Driver objects as arguments.
public function __construct(Driver $driver)
{
$this -> driver = $driver;
}
}
class Driver {}
$driver1 = new Driver();
$car1 = new Car ($driver1);
class Utilis {
// 通过static关键字定义静态属性
static public $numCars = 0;
// 定义静态方法
static public function addToNumCars($int)
{
$int = (int)$int;
self::$numCars += $int;
}
}
// 设置静态属性
Utilis::$numCars = 3;
// 获取静态属性
echo Utilis::$numCars; // 3
// 调用静态方法
Utilis::addToNumCars(3);
echo Utilis::$numCars; // 6
class Utilis {
// 使用php的header方法来让用户跳转到相应的url
static public function redirect($url)
{
header("Location: $url");
exit;
}
}
Utilis::redirect("http://www.notedeep.com");