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 ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

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\n";
        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

NS\ClassName

 

6.

 

posted @ 2015-05-04 12:42  Mirrorhanman  阅读(174)  评论(0编辑  收藏  举报