zoukankan      html  css  js  c++  java
  • The Basics

    http://php.net/manual/zh/language.oop5.basic.php

    1. class

      class contain contants(常量) variables(变量or属性) and function(方法)

    <?php
    class SimpleClass
    {
        // property declaration
        public $var = 'a default value';
    
        // method declaration
        public function displayVar() {
            echo $this->var;
        }
    }
    ?>

      pseudo-variable(伪变量) $this is a reference to the calling object

    2. $this example

    <?php
    class A
    {
        function foo()
        {
            if (isset($this)) {
                echo '$this is defined (';
                echo get_class($this);
                echo ")
    ";
            } else {
                echo "$this is not defined.
    ";
            }
        }
    }
    
    class B
    {
        function bar()
        {
            // Note: the next line will issue a warning if E_STRICT is enabled.
            A::foo();
        }
    }
    
    $a = new A();
    $a->foo();
    
    // Note: the next line will issue a warning if E_STRICT is enabled.
    A::foo();
    $b = new B();
    $b->bar();
    
    // Note: the next line will issue a warning if E_STRICT is enabled.
    B::bar();
    ?>

      the above example will output

    $this is defined (A)
    $this is not defined.
    $this is defined (B)
    $this is not defined.

    3. new

      create an instance of a class

    4. extends

       It is not possible to extend multiple classes

       It is possible to access the overridden methods or static properties by referencing them with parent::.

    <?php
    class ExtendClass extends SimpleClass
    {
        // Redefine the parent method
        function displayVar()
        {
            echo "Extending class
    ";
            parent::displayVar();
        }
    }
    
    $extended = new ExtendClass();
    $extended->displayVar();
    ?>

    5. ::class

      used for class name resolution(类名解析)

      You can get a string containing the fully qualified name of the ClassName class by using ClassName::class.

    <?php
    namespace NS {
        class ClassName {
        }
        B
        echo ClassName::class;
    }
    ?>

      output

    NSClassName

    6.

  • 相关阅读:
    react特点和创建虚拟DOM
    vue的keep-alive
    JavaScript-事件委托
    vue-router参数传递
    js常用的字符串处理
    vue-vuex
    vue-组件
    vue-父子组件传值
    堆和栈
    js-深拷贝浅拷贝
  • 原文地址:https://www.cnblogs.com/iMirror/p/4475905.html
Copyright © 2011-2022 走看看