锟斤拷锟斤拷锟斤拷锟斤拷

class

每锟斤拷锟斤拷亩锟斤拷宥硷拷怨丶锟斤拷锟� class 锟斤拷头锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟皆伙拷锟斤拷锟脚o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟诫方锟斤拷锟侥讹拷锟藉。

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿何凤拷 PHP 锟斤拷锟斤拷锟斤拷锟侥合凤拷锟斤拷签锟斤拷一锟斤拷锟较凤拷锟斤拷锟斤拷锟斤拷锟斤拷母锟斤拷锟铰伙拷锟竭匡拷头锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥革拷锟斤拷锟斤拷只锟斤拷禄锟斤拷摺锟斤拷锟斤拷锟斤拷锟斤拷锟绞斤拷锟绞疚拷锟�[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*锟斤拷

一锟斤拷锟斤拷锟斤拷园锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷约锟斤拷锟�锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为"锟斤拷锟斤拷"锟斤拷锟皆硷拷锟斤拷锟斤拷锟斤拷锟斤拷为"锟斤拷锟斤拷"锟斤拷锟斤拷

Example #1 锟津单碉拷锟洁定锟斤拷

<?php
class SimpleClass
{
    
// 锟斤拷锟斤拷锟斤拷锟斤拷
    
public $var 'a default value';

    
// 锟斤拷锟斤拷锟斤拷锟斤拷
    
public function displayVar() {
        echo 
$this->var;
    }
}
?>

锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟洁定锟斤拷锟节诧拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷一锟斤拷锟斤拷锟矫碉拷伪锟斤拷锟斤拷 $this锟斤拷$this 锟斤拷一锟斤拷锟斤拷锟斤拷锟叫讹拷锟斤拷锟斤拷锟斤拷茫锟酵拷锟斤拷歉梅锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷亩锟斤拷螅锟斤拷锟斤拷锟角从第讹拷锟斤拷锟斤拷锟斤拷锟斤拷态锟斤拷锟斤拷时也锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟襟)★拷

Example #2 $this 伪锟斤拷锟斤拷锟斤拷示锟斤拷

We're assuming that error_reporting is disabled for this example; otherwise the following code would trigger deprecated and strict notices, respectively, depending on the PHP version.

<?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()
    {
        
A::foo();
    }
}

$a = new A();
$a->foo();

A::foo();

$b = new B();
$b->bar();

B::bar();
?>

Output of the above example in PHP 5:

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

Output of the above example in PHP 7:

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

new

要锟斤拷锟斤拷一锟斤拷锟斤拷锟绞碉拷锟斤拷锟斤拷锟斤拷锟绞癸拷锟� new 锟截硷拷锟街★拷锟斤拷锟斤拷锟斤拷锟铰讹拷锟斤拷时锟矫讹拷锟斤拷锟斤拷锟角憋拷锟斤拷值锟斤拷锟斤拷锟角该讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟届函锟斤拷锟斤拷锟斤拷锟节筹拷锟斤拷时锟阶筹拷锟斤拷一锟斤拷锟届常锟斤拷锟斤拷应锟节憋拷实锟斤拷锟斤拷之前锟斤拷锟藉(某些锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟� new 之锟斤拷锟斤拷诺锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟� string锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷实锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷占洌拷锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟狡★拷

Example #3 锟斤拷锟斤拷实锟斤拷

<?php
$instance 
= new SimpleClass();

// 也锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
$className 'Foo';
$instance = new $className(); // Foo()
?>

锟斤拷锟洁定锟斤拷锟节诧拷锟斤拷锟斤拷锟斤拷锟斤拷 new self 锟斤拷 new parent 锟斤拷锟斤拷锟铰讹拷锟斤拷

锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟窖撅拷锟斤拷锟斤拷锟斤拷实锟斤拷锟斤拷锟斤拷一锟斤拷锟铰憋拷锟斤拷时锟斤拷锟铰憋拷锟斤拷锟斤拷锟斤拷锟酵伙拷锟绞碉拷锟斤拷锟斤拷秃锟斤拷酶枚锟斤拷锟街狄伙拷锟斤拷锟斤拷锟斤拷锟轿拷透锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞碉拷锟绞币伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�锟斤拷隆锟斤拷一锟斤拷锟窖达拷锟斤拷锟侥讹拷锟斤拷锟斤拷一锟斤拷锟斤拷实锟斤拷锟斤拷

Example #4 锟斤拷锟斤拷值

<?php

$instance 
= new SimpleClass();

$assigned   =  $instance;
$reference  =& $instance;

$instance->var '$assigned will have this value';

$instance null// $instance and $reference become null

var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

NULL
NULL
object(SimpleClass)#1 (1) {
   ["var"]=>
     string(30) "$assigned will have this value"
}

PHP 5.3.0 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰凤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟绞碉拷锟斤拷锟�

Example #5 锟斤拷锟斤拷锟铰讹拷锟斤拷

<?php
class Test
{
    static public function 
getNew()
    {
        return new static;
    }
}

class 
Child extends Test
{}

$obj1 = new Test();
$obj2 = new $obj1;
var_dump($obj1 !== $obj2);

$obj3 Test::getNew();
var_dump($obj3 instanceof Test);

$obj4 Child::getNew();
var_dump($obj4 instanceof Child);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

bool(true)
bool(true)
bool(true)

PHP 5.4.0 锟金,匡拷锟斤拷通锟斤拷一锟斤拷锟斤拷锟绞斤拷锟斤拷锟斤拷锟斤拷麓锟斤拷锟斤拷锟斤拷锟侥筹拷员锟斤拷

Example #6 锟斤拷锟斤拷锟铰达拷锟斤拷锟斤拷锟斤拷某锟皆�

<?php
echo (new DateTime())->format('Y');
?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�

2016

Properties and methods

Class properties and methods live in separate "namespaces", so it is possible to have a property and a method with the same name. Referring to both a property and a method has the same notation, and whether a property will be accessed or a method will be called, solely depends on the context, i.e. whether the usage is a variable access or a function call.

Example #7 Property access vs. method call

<?php
class Foo
{
    public 
$bar 'property';
    
    public function 
bar() {
        return 
'method';
    }
}

$obj = new Foo();
echo 
$obj->barPHP_EOL$obj->bar(), PHP_EOL;

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

property
method

That means that calling an anonymous function which has been assigned to a property is not directly possible. Instead the property has to be assigned to a variable first, for instance. As of PHP 7.0.0 it is possible to call such a property directly by enclosing it in parentheses.

Example #8 Calling an anonymous function stored in a property

<?php
class Foo
{
    public 
$bar;
    
    public function 
__construct() {
        
$this->bar = function() {
            return 
42;
        };
    }
}

$obj = new Foo();

// as of PHP 5.3.0:
$func $obj->bar;
echo 
$func(), PHP_EOL;

// alternatively, as of PHP 7.0.0:
echo ($obj->bar)(), PHP_EOL;

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

42

extends

一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� extends 锟截硷拷锟街继筹拷锟斤拷一锟斤拷锟斤拷姆锟斤拷锟斤拷锟斤拷锟斤拷浴锟絇HP锟斤拷支锟街讹拷锟截继承o拷一锟斤拷锟斤拷只锟杰继筹拷一锟斤拷锟斤拷锟洁。

锟斤拷锟教承的凤拷锟斤拷锟斤拷锟斤拷锟皆匡拷锟斤拷通锟斤拷锟斤拷同锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角★拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷喽拷宸斤拷锟绞笔癸拷锟斤拷锟� final锟斤拷锟斤拷梅锟斤拷锟斤拷锟斤拷杀锟斤拷锟斤拷恰锟斤拷锟斤拷锟酵拷锟� parent:: 锟斤拷锟斤拷锟绞憋拷锟斤拷锟角的凤拷锟斤拷锟斤拷锟斤拷锟皆★拷

锟斤拷锟斤拷锟角凤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟诫保锟斤拷一锟铰凤拷锟斤拷 PHP 锟斤拷锟斤拷锟斤拷 E_STRICT 锟斤拷锟斤拷拇锟斤拷锟斤拷锟较拷锟斤拷锟斤拷锟斤拷旌拷锟斤拷锟斤拷猓拷锟斤拷旌拷锟斤拷锟斤拷诒锟斤拷锟斤拷锟绞笔癸拷貌锟酵拷牟锟斤拷锟斤拷锟�

Example #9 锟津单碉拷锟斤拷坛锟�

<?php
class ExtendClass extends SimpleClass
{
    
// Redefine the parent method
    
function displayVar()
    {
        echo 
"Extending class\n";
        
parent::displayVar();
    }
}

$extended = new ExtendClass();
$extended->displayVar();
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Extending class
a default value

::class

锟斤拷 PHP 5.5 锟金,关硷拷锟斤拷 class 也锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥斤拷锟斤拷锟斤拷使锟斤拷 ClassName::class 锟斤拷锟斤拷曰锟饺∫伙拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� ClassName 锟斤拷锟斤拷全锟睫讹拷锟斤拷锟狡★拷锟斤拷锟绞癸拷锟斤拷锟� 锟斤拷锟斤拷锟秸硷拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫★拷

Example #10 锟斤拷锟斤拷锟侥斤拷锟斤拷

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

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

NS\ClassName

Note:

The class name resolution using ::class is a compile time transformation. That means at the time the class name string is created no autoloading has happened yet. As a consequence, class names are expanded even if the class does not exist. No error is issued in that case.