search
HomeheadlinesPHP class attributes and class constants

*Class attributes and class constants

* 1. Class attributes only allow the following types of data to be initialized

* Scalar and array literals: strings, values, constants, arrays , Prototype document (php5.3)

* 2. Not allowed to use: variables, expressions, objects

* 3. Class constants are declared using the keyword const, and access restrictions are not allowed to be set , forced to be public and cannot be changed

* 4. Class constants are attribute classes and do not attribute to one of its instance objects. They must be accessed using classes

* 5. Access class constants To use the range parsing character::, double colon

* Use the keyword self in the class to indicate the current class, and the class name can be used directly externally

define('SITE_NAME','PHP中文网');
class User1 
{
    //声明属性
    private $siteName = SITE_NAME;
    private $name = '老顽童';
    private $email = 'lwt@php.cn';
    private $course = ['php','java','python'];
    const LECTURE = '朱老师';
    //构造方法
    public function __construct($name='',$email='', $siteName='',array $course=[]) 
    {
        //如果传参,则使用新值初始化属性,否则使用默认值
        $name ? ($this->name = $name) : $this->name;
        $email ? ($this->email = $email) : $this->email;
        $siteName ? ($this->siteName = $siteName) : $this->siteName;
        $course ? ($this->course = $course) : $this->course;
        
    }
    
    //查询器
    public function __get($name)
    {
        return $this->$name;
    }
    
    //设置器
    public function __set($name,$value)
    {
        return $this->$name = $value;
    }
    
    //在类中访问类常量,使用self来引用当前类名
    public function getConst()
    {
        //类内部也可以直接使用当前类名
//        return User1::LECTURE;
        //推荐使用self:当类名改变时,不必修改内部对它的引用
        return self::LECTURE;
    }
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.