`

PHP5心得4

阅读更多

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方法。
  当一个抽象类的所有方法都是抽象的,那么可以用接口来定义了。本质上来说,接口是一种特殊的抽象类,抽象类中只包含常量和方法的定义,而没有变量和方法的实现。
  比如下面的例子:
 

<?

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);

 

?>

  

 其中声明了接口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了,太方便拉

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics