`
isiqi
  • 浏览: 16079441 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

php5 读书心得

阅读更多

由于工作中要用到PHP,最近下了本《PHP5 POWER PROGRAMMING》在读,外文的,还不错,打算写些读书心得,可能会零散些。这次讲的是多态。

首先看一个例子,是讲动物发出的叫声的。
class Cat {
function miau()
{
print "miau";
}
}

class Dog {
function wuff()
{
print "wuff";
}
}

function printTheRightSound($obj)
{
if ($obj instanceof Cat) {
$obj->miau();
} else if ($obj instanceof Dog) {
$obj->wuff();
} else {
print "Error: Passed wrong kind of object";
}
print "\n";
}

printTheRightSound(new Cat());
printTheRightSound(new Dog());

这里输出的是:
miau(猫的叫声)
wuff(狗的叫声)
熟悉OOP的都知道,这个例子扩展性不好,因为如果你要加入更多动物的话,需要用很多个IF ELSE来判断,而且要重复写很多代码,有了多态后,就好办了。PHP5中终于有多态这东西了,多个子类可以扩展继承父类,上面的例子改写如下:
class Animal {
function makeSound()
{
print "Error: This method should be re-implemented in the children";
}
}

class Cat extends Animal {
function makeSound()
{
print "miau";
}
}

class Dog extends Animal {
function makeSound()
{
print "wuff";
}
}

function printTheRightSound($obj)
{
if ($obj instanceof Animal) {
$obj->makeSound();
} else {
print "Error: Passed wrong kind of object";
}
print "\n";
}

printTheRightSound(new Cat());
printTheRightSound(new Dog());
可以看出,这个时候,无论增加什么动物,printtherightsound方法是不需要 进行任何修改的了!当然,大家学过OOP的可以看出,这个例子可以进一步修改,就是将ANIMAL声明为抽象基类拉。

在OOP中,构造函数和析构函数是很重要的,在PHP4中,可以用和类名相同的方法名称来声明构造函数,比如
class a{
function a()
{

}
}

在PHP5中,是用新的关键字_construct来实现了,而析构函数则用__destruct()
比如一个例子:
class Person {
function __construct($name)
{
$this->name = $name;
}

function getName()
{
return $this->name;
}

private $name;
};

$judy = new Person("Judy") . "\n";
$joe = new Person("Joe") . "\n";

print $judy->getName();
print $joe->getName();

今天讲的是parent::和self::两个新的关键字。self是用来引用当前类并且一般用来访问静态成员变量,parent::用来访问父类,并且一般用来访问父类的构造函数或者方法。有OOP知识的朋友其实很容易理解,以下是例子:
class Ancestor {
const NAME = "Ancestor";
function __construct()
{
print "In " . self::NAME . " constructor\n";
}
}

class Child extends Ancestor {
const NAME = "Child";
function __construct()
{
parent::__construct();
print "In " . self::NAME . " constructor\n";
}
}

$obj = new Child();
输出是:
In Ancestor constructor
In Child constructor

1 instanceof操作
该操作中,判断某个类是否是另一个类的子类或判断是否类的实例。比如考察下面的例子
class Rectangle {
public $name = __CLASS__;
}

class Square extends Rectangle {
public $name = __CLASS__;
}

class Circle {
public $name = __CLASS__;
}

function checkIfRectangle($shape)
{
if ($shape instanceof Rectangle) {
print $shape->name;
print " is a rectangle\n";
}
}

checkIfRectangle(new Square());
checkIfRectangle(new Circle());

其中Square类继承了Rectangle类,而circle类则是单独的。那么,在checkifrectangle中,判断传入来的类是否rectangle的子类。结果输出是'Square is a rectangle\n'.
其中,_CLASS_是一个特殊的常量,表示的是当前类的名称。

2 抽象类和接口
抽象类中,只定义了方法的声明部分,而具体的实现是留给其子类去实现的。比如有如下的例子:

abstract class Shape {
function setCenter($x, $y) {
$this->x = $x;
$this->y = $y;
}

abstract function draw();
protected $x, $y;
}

class Square extends Shape {
function draw()
{
// Here goes the code which draws the Square
...
}
}

class Circle extends Shape {
function draw()
{
// Here goes the code which draws the Circle
...
}
}
其中,形状类是基类,声明了抽象的方法draw,因为不同的图形有不同的“画”的方法。接下来square和circle类都继承了shape类, 并且各自实现了自己的draw方法。
当一个抽象类的所有方法都是抽象的,那么可以用接口来定义了。本质上来说,接口是一种特殊的抽象类,抽象类中只包含常量和方法的定义,而没有变量和方法的实现。
比如下面的例子:

其中声明了接口Loggable,接下来person和product类都实现了其接口,但类A是单独的,没实现其接口,于是在主程序mylog中,分别判断各对象是否loggable的子类。当然,对A,是输出出错信息的。

3 _AUTOLOAD()功能
我们在写OOP时,经常是这样的,先比如在一个目录,如include下,先写了很多个基本的类,然后在其他要应用这些类的程序中,用include("xxx/xxxx.php")的功能,来引入这些类。当工程很大时,有的时候经常会忘记include正确的类文件进来了,而且很容易出错。在PHP5中,增添了_autoload()功能了,比如一个例子如下
MyClass.php
<?php

class MyClass {
function printHelloWorld()
{
print "Hello, World\n";
}
}

?>


general.inc:

<?php

function __autoload($class_name)
{
require_once($_SERVER["DOCUMENT_ROOT"] . "/classes/ $class_name.php");
}

?>


main.php:

<?php

require_once "general.inc";

$obj = new MyClass();
$obj->printHelloWorld();

?>
其中,假设在/classes目录下,每个类假设以同名的方式命名PHP文件,如一个叫a的类,以a.php文件来命名。在上面的例子中,在main.php中,没有显式生命类myclass,但是require了general.inc,在这个INC文件中,使用了_autoload方法,自动去classes目录下,找相应的类,这里找到了myclass.php了。看,这样就很方便了,不需要在main.php中,大量写很多的include了,太方便拉

<?

interface Loggable {
function logString();
}

class Person implements Loggable {
private $name, $address, $idNumber, $age;
function logString() {
return "class Person: name = $this->name, ID = $this >idNumber\n";
}
}

class Product implements Loggable {
private $name, $price, $expiryDate;
function logString() {
return "class Product: name = $this->name, price = $this >price\n";
}
}
class a
{
function printa()
{
print "hello";
}
}

function MyLog($obj) {
if ($obj instanceof Loggable) {
print $obj->logString();
} else {
print "Error: Object doesn't support Loggable interface\n";
}
}

$person = new Person();
// ...
$product = new Product();
$a=new a();
MyLog($person);
MyLog($product);
MyLog($a);

?>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics