zoukankan      html  css  js  c++  java
  • PHP面向对象

    PHP类的成员定义

    <?php
    class Point
    {
        //静态成员,类内被使用self::$size,在类外Point::size
        static $size=10;
        //常量,使用时可以不用$
        const PI=301415926//私有字段
        private $id;
        private $x;
        private $y;
        //使用自定义的方法进行访问控制
        function getId()
        {
            return $this->id;
        }
        //提供对私有字段的访问控制
        function __get($property_name)
        {
            if(isset($this->$property_name))
            {
                return $this->$property_name;
            }
            else
            {
                return NULL;
            }
        }
        //提供对私有字段的写入控制
        function __set($property_name,$value)
        {
            if(is_double($value))
            {
                $this->$property_name=value;
            }
            else
            {
                echo "error: value is not a double number.";
            }
        }
        //构造函数
        function __construct($x,$y,$id=0)
        {
            $this->x=$x;
            $this->y=$y;
            $this->id=$id;
        }
        function Print_info()
        {
            echo $this->id,"<br>";
            echo $this->x,"<br>";
            echo $this->y,"<br>";
        }
    //析构函数 functionfunction __destruct() { echo "destruct function"; } } ?>

    抽象类

    抽象类不能被实例化,用于为继承的子类定义接口,包含有属性和方法,其中必须有一个抽象方法。

    继承

    继承自抽象类的抽象方法,必须在子类中被重写。被重写的方法必须参数个数应该相同,可以有可选参数。

    使用final修饰类,或者方法,这样就不能被继承或重写。

    <?php
    abstract class Object
    {
        abstract public function Output();
        final public function Msg()
        {
            echo "object msg","<br>";
        }
    }
    final class Point extends Object
    {
        public function Output()
        {
            //调用基类的方法,当然可以$this->Msg(),方法被覆盖时就不能用了吧!
            parent::Msg();
            echo "point","<br>";
        }
    }
    class Rectangle extends Object
    {
        public function Output()
        {
            echo "Rectangle","<br>";
        }
    }
    //多态性,限制传入的类型为Object
    function runObject(Object $obj)
    {
        $obj->Output();
    }
    runObject(new Point());
    runObject(new Rectangle());
    ?>

    接口

    <?php
    interface IUser
    {
        public function addUser();
    }
    class Administrator implements IUser
    {
        public function addUser()
        {
            echo "add user";
        }
    }
    (new Administrator())->addUser();
    ?>

     内置对象函数 

    • __autoload — 尝试加载未定义的类 | Attempt to load undefined class
    • call_user_method_array — 调用一个用户方法,同时传递参数数组(已废弃) | Call a user method given with an array of parameters
    • call_user_method — 对特定对象调用用户方法(已废弃) | Call a user method on an specific object
    • class_alias — 为一个类创建别名 | Creates an alias for a class
    • class_exists — 检查类是否已定义 | Checks if the class has been defined
    • get_called_class — 后期静态绑定("Late Static Binding")类的名称 | the "Late Static Binding" class name
    • get_class_methods — 返回由类的方法名组成的数组 | Gets the class methods' names
    • get_class_vars — 返回由类的默认属性组成的数组 | Get the default properties of the class
    • get_class — 返回对象的类名 | Returns the name of the class of an object
    • get_declared_classes — 返回由已定义类的名字所组成的数组 | Returns an array with the name of the defined classes
    • get_declared_interfaces — 返回一个数组包含所有已声明的接口 | Returns an array of all declared interfaces
    • get_declared_traits — 返回所有已定义的 traits 的数组 | Returns an array of all declared traits
    • get_object_vars — 返回由对象属性组成的关联数组 | Gets the properties of the given object
    • get_parent_class — 返回对象或类的父类名 | Retrieves the parent class name for object or class
    • interface_exists — 检查接口是否已被定义 | Checks if the interface has been defined
    • is_a — 如果对象属于该类或该类是此对象的父类则返回 TRUE | Checks if the object is of this class or has this class as one of its parents
    • is_subclass_of — 如果此对象是该类的子类,则返回 TRUE | Checks if the object has this class as one of its parents or implements it.
    • method_exists — 检查类的方法是否存在 | Checks if the class method exists
    • property_exists — 检查对象或类是否具有该属性 | Checks if the object or class has a property
    • trait_exists — 检查指定的 trait 是否存在 | Checks if the trait exists
  • 相关阅读:
    hdu5728 PowMod
    CF1156E Special Segments of Permutation
    CF1182E Product Oriented Recurrence
    CF1082E Increasing Frequency
    CF623B Array GCD
    CF1168B Good Triple
    CF1175E Minimal Segment Cover
    php 正则
    windows 下安装composer
    windows apache "The requested operation has failed" 启动失败
  • 原文地址:https://www.cnblogs.com/qiusuo/p/5127604.html
Copyright © 2011-2022 走看看