Blogger Information
Blog 42
fans 0
comment 0
visits 36473
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类常量的定义和访问
庆选的博客
Original
1359 people have browsed it

1、类常量的命名 以关键字const 命名  eg:const NATION = '中国';

2、访问:在实例化类后,使用静态访问符号::访问  eg:

$test=new Demo2('男');
echo $test::NATION;

类常量和静态属性均需要使用::访问

实例

<?php

namespace _1008;

//类常量
class Demo2
{
    // 类常量
    const NATION = '中国';

    // 类属性
    public static $sex;

    public function __construct($sex)
    {
        self::$sex = $sex;
    }

    public function get()
    {
        return self::NATION;
    }


}
$test=new Demo2('男');
echo $test::NATION;
echo '<hr>';
echo $test::$sex;
echo '<hr>';
echo $test->get();

运行实例 »

点击 "运行实例" 按钮查看在线实例

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post