zoukankan      html  css  js  c++  java
  • PHP面向对象重要知识点----------第一部分

    1. __construct: 

         内置构造函数,在对象被创建时自动调用。见如下代码:

    <?php
    class ConstructTest {
        private $arg1;
        private $arg2;
    
        public function __construct($arg1, $arg2) {
            $this->arg1 = $arg1;
            $this->arg2 = $arg2;
            print "__construct is called...
    ";
        }
        public function printAttributes() {
            print '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."
    ";
        }
    }
    $testObject = new ConstructTest("arg1","arg2"); 
    $testObject->printAttributes();

    2. parent: 

         用于在子类中直接调用父类中的方法,功能等同于Java中的super。 

    <?php
    class BaseClass {
    protected $arg1;
    protected $arg2;

    function __construct($arg1, $arg2) {
    $this->arg1 = $arg1;
    $this->arg2 = $arg2;
    print "__construct is called... ";
    }
    function getAttributes() {
    return '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2;
    }
    }

    class SubClass extends BaseClass {
    protected $arg3;

    function __construct($baseArg1, $baseArg2, $subArg3) {
    parent::__construct($baseArg1, $baseArg2);
    $this->arg3 = $subArg3;
    }
    function getAttributes() {
    return parent::getAttributes().' $arg3 = '.$this->arg3;
    }
    }
    $testObject = new SubClass("arg1","arg2","arg3");
    print $testObject->getAttributes()." ";

    3. self:

         在类内调用该类静态成员和静态方法的前缀修饰,对于非静态成员变量和函数则使用this。 

    复制代码
  • 相关阅读:
    Charles关于Https SSLHandshake解决备忘录
    图片Image转换为base64编码的方法
    图解HTTP(三)
    图解HTTP(二)
    图解HTTP(一)
    Linux下which、whereis、locate、find命令作用
    jQuery实现图片上传
    常见前端面试题备注
    css实现正方形div的3种方式
    promise
  • 原文地址:https://www.cnblogs.com/cgdblog/p/7325559.html
Copyright © 2011-2022 走看看