php面向对象

来源:互联网 发布:淘宝开店身份验证 编辑:程序博客网 时间:2024/06/06 20:04


1. 类的创建


创建一个类 man.php;

<?phpclass man{    /**     * @param string $name     * @param int $age     */    private $name, $age;    public function __construct($name, $age){        $this->name = $name;        $this->age = $age;        echo 'construct';    }    /**     * @return mixed     */    public function getAge()    {        return $this->age;    }    /**     * @param mixed $age     */    public function setAge($age)    {        $this->age = $age;    }    /**     * @return mixed     */    public function getName()    {        return $this->name;    }    /**     * @param mixed $name     */    public function setName($name)    {        $this->name = $name;    }}


对这个类进行实例化;

require_once 'man.php';$m = new man('zhang', 3);echo $m->getName();

2. 类的命名空间;

在工程中,会出现相同类名的类,这种情况下通过不同的命名空间区分;

定义两个相同类名的类,分别放在两个目录下

<?phpnamespace thystar;class hello1{    public function say(){        echo 'hello';    }}

<?phpnamespace thy;class hello1{    public function say(){        echo 'hello thy';    }}

使用这两个类;

require_once 'thystar/hello1.php';require_once 'thy/hello1.php';$t1 = new \thystar\hello1();$t2 = new \thy\hello1();$t1->say();$t2->say();


3. 成员方法和类方法

成员方法:定义在类中的方法,如setter和getter等;

类方法 : 用static定义的方法;即静态方法,执行时不需要创建实例用“类::方法”执行

eg: 在上面的man类中定义:

public static function say(){        echo 'I am a man';    }

调用这个方法:

man::say();

类方法的使用场合: 描述属性。


静态的常量,变量,及静态的方法都是用于描述设计信息的,如:

对man这个类做如下修改;

<?phpclass man{    /**     * @param string $name     * @param int $age     */    private $name, $age;    public function __construct($name, $age){        $this->name = $name;        $this->age = $age;        //echo 'construct';        man::$NUM++        if(man::$NUM > man::MAX_NUM){//限制人数;            throw new Exception("over");        }    }    /**     * @return mixed     */    public function getAge()    {        return $this->age;    }    /**     * @param mixed $age     */    public function setAge($age)    {        $this->age = $age;    }    /**     * @return mixed     */    public function getName()    {        return $this->name;    }    /**     * @param mixed $name     */    public function setName($name)    {        $this->name = $name;    }    public static function say(){//静态方法        echo 'I am a man';    }    //创建静态常量    private static $NUM = 0;    const MAX_NUM = 200;}

调用:

for($i=0; $i<200; $i++){    new man('thystar', 10);}

4. 类的重构和方法重写;

首先,定义一个People类(对之前的man类修改):

<?php/** * Created by PhpStorm * Date: 2015/8/5 * Time: 22:30 */class People{    /**     * @param string $name     * @param int $age     * @param string $sex     */    private $name, $age, $sex;    public function __construct($name, $age, $sex){        $this->name = $name;        $this->age = $age;        $this->sex = $sex;        //echo 'construct';        People::$NUM++;        if(People::$NUM > People::MAX_NUM){            throw new Exception("over");        }    }    /**     * @return mixed     */    public function getSex()    {        return $this->sex;    }    /**     * @param mixed $sex     */    public function setSex($sex)    {        $this->sex = $sex;    }    /**     * @return mixed     */    public function getAge()    {        return $this->age;    }    /**     * @param mixed $age     */    public function setAge($age)    {        $this->age = $age;    }    /**     * @return mixed     */    public function getName()    {        return $this->name;    }    /**     * @param mixed $name     */    public function setName($name)    {        $this->name = $name;    }    public function sayhi(){        echo $this->name;    }    public static function say(){        echo 'Hi';    }    //创建静态常量    private static $NUM = 0;    const MAX_NUM = 200;}

定义继承类Man;

<?phprequire_once 'People.php';class Man extends People{    public function __construct($name,$age ){        parent::__construct($name, $age, 'm');    }    //重写方法    public function sayhi(){        echo 'I am a man';    }}


方法调用:

require_once 'Man.php';$man = new Man('zzzzz', 11);$man->sayhi();









0 0
原创粉丝点击