面向过程和面向对象
1. 历史因素
- 只要是高级编程语言,面向对象永远都是绕不开的话题。早期的编程都是为了追求速度,而没有考虑可理解性和可扩展性。现在我们考虑的更多是可维护性和重用性。
- PHP2:PHP其实一开始也没考虑对象,他是PHP的创建者们后来才想要引进的,PHP起源于Rasmus Lerdorf用Perl开发的两个工具,这时候PHP是 Personal Homepage Tool的简写,意为“个人主页工具”,而FI就是 From Interpreter“表单解释器”。只是用来处理表单数据到数据库,简单的控制下流程。
- PHP3:此时PHP还是没有加入对类的支持. 1997.8.27 加入的
- PHP4:算是PHP比较重大的改变,此时PHP的大部分核心代码都被重写了。Zend引擎开始为PHP提供助力
- PHP5:PHP开始拥抱未来,支持了面向对象的程序设计
- PHP7:弱语言类型也可以声明变量的类型,效率也大大提升了
2. 面向过程
我们用编写的程序来完成一件事/一个功能,需要哪些功能就建立哪些函数。管理起来非常混乱
3. 面向对象
我们不在仅仅只是想着去完成这件事就好了,将需要完成的事情分配给对象,每个对象各司其职。对象是执行者,干事的还是函数。这样我们可以更好的描述在解决问题和完成这件事中的行为,明显更符合人的思维习惯,同时也大大提升了代码的重用性和扩展性。
对象基础
1. 类和对象
- 类:人类、车、宠物,我们可以按照人的划分习惯,将某一些具有相同特性的都是抽象化,这就是类。这就像是一个集合的概念。
- 对象:是类的实例,比如人有很多,有人叫张三有人叫李四,他可能就有唯一一张ID卡,我们根据某些特性将类中的集合分离成一个个,对象是个体的概念。
1.1 最简单的类和对象
<?php
// 如,我们可以编写出一个,最简单的类
class ShopProduct
{
// 类体
}
// 对象
$product1 = new ShopProduct();
$product2 = new ShopProduct();
// 我很好奇这样一个几乎为空的对象有什么
var_dump($product1); // object(ShopProduct)[1]
// 除了告诉我这个一个对象,和是谁创建的模板也没啥了,哈哈
1.2 设置属性
<?php
class ShopProduct
{
// 此时,我们设置了4个属性
public $title = 'default product';
public $producerMainName = 'main name';
public $producerFirstName = 'first name';
public $price = 0;
}
// 访问其中的 title 属性
print ((new ShopProduct)->title) . '<br>'; // default product
// 也可以设置 属性的值
$product1 = new ShopProduct();
$product1->title = 'My Antonia';
var_dump($product1);
/*
object(ShopProduct)[2]
public 'title' => string 'My Antonia' (length=10)
public 'producerMainName' => string 'main name' (length=9)
public 'producerFirstName' => string 'first name' (length=10)
public 'price' => int 0
*/
// 实际上PHP没有强制所有属性都必须在类中声明,我们也可以动态的给对象添加属性
$product1->arbitaryAddition = 'treehouse';
var_dump($product1);
// 但这并不是一个好的习惯,我们几乎不用。动态添加的属性可能会遇到种种问题。
/*
object(ShopProduct)[1]
public 'title' => string 'My Antonia' (length=10)
public 'producerMainName' => string 'main name' (length=9)
public 'producerFirstName' => string 'first name' (length=10)
public 'price' => int 0
public 'arbitaryAddition' => string 'treehouse' (length=9)
*/
1.3 设置方法
<?php
class ShopProduct
{
public $title = 'default product';
public $producerMainName = 'main name';
public $producerFirstName = 'first name';
public $price = 0;
// 这里我们并没有添加,访问权限符号,因为默认的就是 public
function getProducer()
{
return $this->producerMainName . '_' . $this->producerFirstName;
}
}
$product1 = new ShopProduct();
echo $product1->getProducer() . '<br>'; // main name_first name
1.4 设置构造函数
<?php
class ShopProduct
{
public $title;
public $producerMainName;
public $producerFirstName;
public $price = 0;
/**
* 我们添加构造方法,创建对象时构造方法会被自动调用。
* PHP5之前,构造方法使用和类相同的名字,像是java一样。
* 但现在我们一般, __construct()
**/
public function __construct($title, $firstName, $mianName, $price)
{
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mianName;
$this->price = $price;
}
public function getProducer()
{
return $this->producerMainName . '_' . $this->producerFirstName;
}
}
$procuct1 = new ShopProduct('My Antonia', 'Willa', 'Cather', 5.99);
var_dump($procuct1);
/*
object(ShopProduct)[1]
public 'title' => string 'My Antonia' (length=10)
public 'producerMainName' => string 'Cather' (length=6)
public 'producerFirstName' => string 'Willa' (length=5)
public 'price' => float 5.99
*/
1.5 PHP中的基本数据类型
- 数据类型:PHP中也有八大基本数据类型,我也已经实在是不想写了。权当复习
- 布尔型 boolead is_bool()
- 整形 integer is_integer()
- 浮点型 float is_double()
- 字符型 strting is_string()
- 数组 array is_array()
- 对象 object is_object()
- 资源 resource is_resource()
- 空类型 null is_null()
1.6 继承
- 继承是一从一个基类获取到更多派生类的机制
- 基类也叫做父类,继承基类的就是子类,子类就可以增加父类之外的功能,这就是扩展性
<?php
// 如果我们有两种产品,我们将所有的属性都设置在一个类中,会显得十分臃肿。如下
class ShopProduct
{
public $numPages;
public $playLength;
public $title;
public $producerMainName;
public $producerFirstName;
public $price;
function __construct($title, $firstName, $mianName, $price, $numPages = 0, $playLength = 0)
{
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mianName;
$this->price = $price;
$this->numPages = $numPages;
$this->playLength = $playLength;
}
function getNumberOfPages()
{
return $this->numPages;
}
function getPlayLength()
{
return $this->playLength;
}
function getProducer()
{
return $this->producerFirstName . $this->producerMainName;
}
}
<?php
// 如果分开变成两个类,就会发现解决了不存在的属性。但是大量的代码重复还是让人感觉不是很愉快
class cdProduct
{
public $playLength;
public $title;
public $producerFirstName;
public $producerMainName;
public $price;
function __construct($title, $firstName, $mianName, $price, $playLength)
{
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mianName;
$this->price = $price;
$this->playLength = $playLength;
}
function getPlayLength ()
{
return $this->playLength;
}
function getSummaryLine()
{
$base = "{$this->title} | {$this->producerMainName} , ";
$base .= "{$this->producerFirstName} , ";
$base .= ": playing time - {this->playLength}";
return $base;
}
function getProducer()
{
return $this->producerFirstName . $this->producerMainName;
}
}
class BookProduct
{
public $numPages;
public $title;
public $producerMainName;
public $producerFirstName;
public $price;
function __construct($numPages, $title, $firstName, $mianName, $price)
{
$this->numPages = $numPages;
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mianName;
$this->price = $price;
}
function getNumberOfPages()
{
return $this->numPages;
}
function getSummaryLine()
{
$base = "{$this->title} | {$this->producerMainName} , ";
$base .= "{$this->producerFirstName} , ";
$base .= ": playing time - {this->playLength}";
return $base;
}
function getProducer()
{
return $this->producerFirstName . $this->producerMainName;
}
}
<?php
// 使用继承 - 父类产品
class ShopProduct
{
public $numPages;
public $playLength;
public $title;
public $producerMainName;
public $producerFirstName;
public $prrice;
function __construct($numPages = 0, $title, $playLength = 0, $firstNmae, $mianName, $price)
{
$this->title = $title;
$this->numPages = $numPages;
$this->producerFirstName = $firstNmae;
$this->producerMainName = $mianName;
$this->playLength = $playLength;
$this->price = $price;
}
function getProducer()
{
return $this->producerFirstName . $this->producerMainName;
}
function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName;
}
}
// 子类-cd
class CdProduct extends ShopProduct
{
function getPlayLength()
{
return $this->playLength;
}
function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName . ',' .
$this->playLength;
}
}
// 子类书
class BookProduct extends ShopProduct
{
function getNumberOfPages()
{
return $this->numPages;
}
function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName . ',' .
$this->numPages;
}
}
// 两个子类都会继承父类中的公共部分
- 在子类中定义构造方法时需要传递参数给父类的构造方法,否则你得到的可能是一个不完整的对象
- 子类调用父类的方法,使用parent 关键字 parent::__construct();
- 我们依照parent关键字去添加子类的构造函数,并且从新改造父类和子类。
- 我们通常把子类会用到的相同的属性提取到父类
- 子类再去创建自己独有的属性,因为子类也可以访问到父类的属性
<?php
class ShopProduct
{
public $title;
public $producerMainName;
public $producerFirstName;
public $prrice;
function __construct($title,$firstNmae, $mianName, $price)
{
$this->title = $title;
$this->producerFirstName = $firstNmae;
$this->producerMainName = $mianName;
$this->price = $price;
}
function getProducer()
{
return $this->producerFirstName . $this->producerMainName;
}
function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName;
}
}
class CdProduct extends ShopProduct
{
public $playLength;
function __construct($title,$firstNmae, $mianName, $price, $playLength)
{
parent::__construct($title,$firstNmae, $mianName, $price);
$this->playLength = $playLength;
}
function getPlayLength()
{
return $this->playLength;
}
function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName . ',' .
$this->playLength;
}
}
class BookProduct extends ShopProduct
{
public $numPages;
function __construct($title,$firstNmae, $mianName, $price, $numPages)
{
parent::__construct($title,$firstNmae, $mianName, $price);
$this->numPages = $numPages;
}
function getNumberOfPages()
{
return $this->numPages;
}
function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName . ',' .
$this->numPages;
}
}
- 此时,结构清晰了很多,但我们还是发现,子类和父类还是有 getSummaryLine()这个方法
- 重写这个方法是我们也 用上 parent 关键字
<?php
class ShopProduct
{
public $title;
public $producerMainName;
public $producerFirstName;
public $prrice;
function __construct($title, $firstNmae, $mianName, $price)
{
$this->title = $title;
$this->producerFirstName = $firstNmae;
$this->producerMainName = $mianName;
$this->price = $price;
}
function getProducer()
{
return $this->producerFirstName . $this->producerMainName;
}
function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName;
}
}
class CdProduct extends ShopProduct
{
public $playLength;
function __construct($title, $firstNmae, $mianName, $price, $playLength)
{
parent::__construct($title, $firstNmae, $mianName, $price);
$this->playLength = $playLength;
}
function getPlayLength()
{
return $this->playLength;
}
function getSummaryLine()
{
$base = parent::getSummaryLine();
return $base . 'playing time:' . $this->playLength;
}
}
class BookProduct extends ShopProduct
{
public $numPages;
function __construct($title, $firstNmae, $mianName, $price, $numPages)
{
parent::__construct($title, $firstNmae, $mianName, $price);
$this->numPages = $numPages;
}
function getNumberOfPages()
{
return $this->numPages;
}
function getSummaryLine()
{
$base = parent::getSummaryLine();
return $base . 'number of pages:' . $this->numPages;
}
}
1.7 类的访问管理
三种基本的权限访问修饰符
- public:公共的,任何地方都可以访问public修饰的属性和方法
- protected:受保护的,只有当前类和子类才可以访问
- private:私有的,只有当前类才可以访问,子类也不可以访问
小技巧
- private 、 protectedt 通常是用来保护用的,我们通常用 private 来修饰属性让外部无法去更改,从而达到保护的作用
- 简而言之,有些类方法和类属性本身只为该类提供方法,不对外提供,我们就应该设置权限为 private 或者是protected的
我们依照权限访问符,加入商品打折的属性,从写改写一下 父类和子类
父类-商品类
<?php
class ShopProduct
{
// 私有化属性
private $title;
private $producerMainName;
private $producerFirstName;
private $discount = 1; // 1 就表示不打折
protected $prrice;
// 构造函数
public function __construct($title, $firstNmae, $mianName, $price)
{
$this->title = $title;
$this->producerFirstName = $firstNmae;
$this->producerMainName = $mianName;
$this->price = $price;
}
// 获取商品的 firstName
public function getProducerFirstName()
{
return $this->producerFirstName;
}
// 获取商品的 mainName
public function getProducerMainName()
{
return $this->producerMainName;
}
// 获取商品的title
public function getTitle()
{
return $this->title;
}
// 获取商品的价格
public function getPrice()
{
return $this->prrice;
}
// 获取折扣
public function getDisCount()
{
return $this->discount;
}
// 设置折扣
public function setDisCount($num)
{
$this->discount = $num;
}
// getProducer
public function getProducer()
{
return $this->producerFirstName . $this->producerMainName;
}
// getSummaryLine
public function getSummaryLine()
{
return $this->title . ':' .
$this->producerMainName . ',' .
$this->producerFirstName;
}
}
子类-书
<?php
class BookProduct extends ShopProduct
{
private $numPages;
function __construct($title, $firstNmae, $mianName, $price, $numPages)
{
parent::__construct($title, $firstNmae, $mianName, $price);
$this->numPages = $numPages;
}
function getNumberOfPages()
{
return $this->numPages;
}
function getSummaryLine()
{
$base = parent::getSummaryLine();
return $base . 'number of pages:' . $this->numPages;
}
}
子类-cd
<?php
class CdProduct extends ShopProduct
{
private $playLength;
function __construct($title, $firstNmae, $mianName, $price, $playLength)
{
parent::__construct($title, $firstNmae, $mianName, $price);
$this->playLength = $playLength;
}
function getPlayLength()
{
return $this->playLength;
}
function getSummaryLine()
{
$base = parent::getSummaryLine();
return $base . 'playing time:' . $this->playLength;
}
}
2. 测试访问权限修饰符
2.1 测试一 对象访问本类的非静态属性和非静态方法
```php <?php class Father { public $a = ‘a 属性’; protected $b = ‘b 属性’; private $c = ‘c 属性’;
public function a() {
echo "function a <br/>";
}
protected function b() {
echo "function b <br/>";
}
private function c() {
echo "function c <br/>";
} }
$fa = new Father(); echo $fa->a; // a 属性 // echo $fa->b; // Fatal error: Uncaught Error: Cannot access protected property Father::$b // echo $fa->c; // Fatal error: Uncaught Error: Cannot access private property Father::$c
$fa->a(); // function a // $fa->b(); // Fatal error: Uncaught Error: Call to protected method Father::b() // $fa->c(); // Fatal error: Uncaught Error: Call to private method Father::c()
<a name="ic3Vt"></a>
#### 2.2 测试二 本类访问静态属性和静态方法
```php
<?php
class Father
{
public static $a = 'a 属性';
protected static $b = 'b 属性';
private static $c = 'c 属性';
public static function a()
{
echo "function a <br/>";
}
protected static function b()
{
echo "function b <br/>";
}
private static function c()
{
echo "function c <br/>";
}
}
echo Father::$a; // a 属性
// echo Father::$b; // Fatal error: Uncaught Error: Cannot access protected property Father::$b
// echo Father::$c; // Fatal error: Uncaught Error: Cannot access private property Father::$c
Father::a(); // function a
// Father::b(); // Uncaught Error: Call to protected method Father::b()
// Father::c(); // Fatal error: Uncaught Error: Call to private method Father::c()
2.3 测试三 子类访问父类的属性和方法
<?php
//父类
class Father
{
public $a = 'a 属性';
protected $b = 'b 属性';
private $c = 'c 属性';
public function a()
{
echo "function a <br/>";
}
protected function b()
{
echo "function b <br/>";
}
private function c()
{
echo "function c <br/>";
}
}
class Son extends Father
{
public function method_1()
{
echo parent::$a . '<br>';
}
public function method_2()
{
echo parent::$b . '<br>';
}
public function method_3()
{
echo parent::$c . '<br>';
}
public function method_4()
{
parent::a();
}
public function method_5()
{
parent::b();
}
public function method_6()
{
parent::c();
}
}
$son = new Son();
// $son->method_1(); // Fatal error: Uncaught Error: Access to undeclared static property: Father::$a
// $son->method_2(); // Fatal error: Uncaught Error: Access to undeclared static property: Father::$b
// $son->method_3(); // Fatal error: Uncaught Error: Cannot access private property Father::$c
$son->method_4(); // function a
$son->method_5(); // function b
// $son->method_6(); // Fatal error: Uncaught Error: Call to private method Father::c() from context 'Son'
2.4 测试小结
- 对象无论是访问本类的非静态的属性还是访问非静态的方法都只能访问 public ,protected和privated 都不行
- 为什么PHP中,本类创建的对象连protected修饰的方法和属性都无法访问?
- 这是我自己理解的问题!!!! 使用方式就像局部变量定义,但是超出了使用范围
上一条对于静态修饰的方法和属性一样适用
子类使用parent 关键字只能访问父类的 public 方法和 protected方法,并不能访问非静态的属性
- parent :: 那么对于子类访问父类的静态属性呢?
- public 和 protected 可以访问到
- privated 访问不到
<?php
class Father
{
public $a = 'a 属性';
protected $b = 'b 属性';
private $c = 'c 属性';
public function a()
{
echo "function a <br/>";
}
protected function b()
{
echo "function b <br/>";
}
private function c()
{
echo "function c <br/>";
}
}
class Son extends Father
{
public function index()
{
$son = new Son();
$son->b(); // al
}
}
$son = new Son();
$son->index(); // function b