zoukankan      html  css  js  c++  java
  • 【五】php 面向对象

    面向对象

    概念:按类进行分类,类是表示彼此之间可能互不相同,但是具有一些共同点的对象集合
    多态性:不同的类对同一操作可以有不同的行为
    继承:允许我们使用子类在类之间创建层次关系
    关键字:class
    结构 :class classname{ }
    作用域关键字:public、private、protected
    构造函数:初始化(___construct($params))
    析构函数:与构造函数相对。在所有该类的引用都会被重置或超出作用域时自动发生(__destruct)
    类的实例化:new
    class main{
    function __construct($params)
    {
        echo $params;
    }
    }
     
    $a=new main("123"); //输出123
    $b=new main(); //输出空
    ps:___construct要跟new 带的参数一致
    通过函数来实现对属性的访问:
    __set:当外部无法访问某个类变量时,通过该方法设置值
    __get:当外部无法访问某个类变量时,通过该方法获取值
    class main{
    private $name;
    private $age;
    public function __construct($name="",$age=18)
    {
    $this->name=$name;
    $this->age=$age;
    }
     
    function __get($name)
    {
        // TODO: Implement __get() method.
        return $this->$name;
    }
    function __set($name, $value)
    {
        // TODO: Implement __set() method.
        $this->name=$value;
    }
    function say(){
        echo $this->name.$this->age;
    }
    }
     
    $a=new main("cmf",18);
    $a->name="hello"; //如果没有__set,则会出错
    echo $a->age; //如果没有__get,则会出错
    使用private和public关键字控制访问
    • 默认是public
    • public:公有属性。类的内部和外部都可以访问
    • private:私有属性。只能在类的内部访问。使用__set\__get可以在外部使用该属性
    • protected:只能在内部使用

    继承

    关键字:extends
    ps:继承是单方向的。子类可以从父类或超类继承特性(public、protected)
    通过private和protected访问修饰符来控制需要继承的内容
    • public:公有属性,继承类外部和内部都可以使用
    • private:无法被子类继承(方法和属性)
    • protected:被继承,只能在子类内部用,在外部无法使用
    class main{
    public $name;
    private $age;
    protected $money;
    protected function say(){
        echo "say";
    }
    private function run(){
        echo "run";
    }
    public function hair(){
        echo "hair";
    }
    }
    class test extends main{
    function __construct()
    {
        this->name="test";
        $this->age=18; //致命错误,无法继承
        $this->money="more and more";
        $this->say();
        $this->run(); //致命错误
        $this->hair();
    }
    }
    $cmf=new test();
    重载:重新定义父类的属性及方法
    class a{
    public $attribute="a";
    function oreration(){
        echo $this->attribute;
    }
    }
    class b extends a{
        public $attribute="b";
        function oreration()
        {
            parent::oreration(); //输出b
            echo "hello";
        }
    }
    $b=new b();
    $b->oreration();        
    禁止继承和重载:final
    class a{
        public $attribute="a";
        final function oreration(){
            echo $this->attribute;
        }
    }
    class b extends a{
        public $attribute="b";
        function oreration(){} //致命错误,无法重载final的方法
    }
    实现接口:可以解决多重继承的问题,一个类可以继承一个类,但是可以实现一个或多个接口
    interface Displayable{
        function say(); //没有花括号
    }
    class test implements Displayable{
        function say() //如果没有实现接口中的制定方法,会发生致命错误
        {
            echo "hello";
        }
    }    
    静态方法及per-class常量
    • 在没有实例化类的情况下可以访问
    • 在一个静态方法中,不能使用this关键字
    class Math{
    const pi=3.14159;
        static function squared($input){
        $this->name='123'; //致命错误
        return $input*$input;
        }
    }
    echo Math::pi; //输出3.14159
    echo Math::squared(8); //输出64
    检查类的类型和类型提示:instanceof
    • 检查一个对象是否为特定类的实例
    interface a{
        function say();
    }
    class aa implements a{
        function say(){}
    }
    class bb {}
    $aa=new aa();
    $bb=new bb();
    echo $aa instanceof a; //true
    echo $bb instanceof a; //false
    克隆对象:clone
    • 复制一个已有的对象
    • 如果不需要克隆过来的默认属性,可以在基类中创建一个__clone()方法
    抽象类:
    abstract class a{
        abstract function func($param1,$param2);
    }
    重载方法:__call
    • 必须带2个参数,一个方法名称,一个参数
    /**
    * __call方法是调用不可见(无权限或不存在)的方法时,自动调用
    * $aa->hello(123);
    * 没有hello方法,调用call("hello",'123')
    */
    class a{
        public function __call($name, $arguments)
        {
            echo '你想调用我不存在的方法', $name, '方法';
           echo '还传了一个参数';
            print_r($arguments);
        }
    }
    $aa=new a();
    $aa->hello(123); //输出:你想调用我不存在的方法hello方法还传了一个参数Array ( [0] => 123 )
    实现迭代器和迭代
    • 通过forrach方法循环取出一个对象的所有属性
    class myclass{
        public $a=5;
        public $aa=55;
        public $aaa=555;
        public function hello{
        echo "hello"; //致命错误 ,只能取属性
        }
    }
    $x=new myclass();
    foreach ($x as $attribute){
        echo $attribute."<br/>";
    }
    将类转换成字符串:__toStirng
    class test{
        public $foo="hello";
        public function __toString()
        {
            return(var_export($this,true)); //输出所有的属性
            // return $this->foo;
        }
    }
    $a=new test();
    echo $a; //输出test::__set_state(array( 'foo' => 'hello', );
     
  • 相关阅读:
    hi.baidu.com 百度流量统计
    Autofac is designed to track and dispose of resources for you.
    IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
    How to create and manage configuration backups in Internet Information Services 7.0
    定制swagger的UI
    NSwag在asp.net web api中的使用,基于Global.asax
    NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
    JS变量对象详解
    JS执行上下文(执行环境)详细图解
    JS内存空间详细图解
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/7819272.html
Copyright © 2011-2022 走看看