What attributes does the php class have?

silencement
Release: 2023-02-25 11:42:02
Original
3271 people have browsed it

What attributes does the php class have?

The variable members of the class are called attributes. The attribute declaration starts with the keywords Public, Protected, and Private, and is followed by an ordinary variable declaration. The variables in the attribute can

Initialization, but the initialized value must be a constant.

If you use var to declare attributes directly without using public, protected or private, PHP will treat it as public.

In the member method of the class, you can use -> (object operator) such as $this->property (property is the property name) to access non-static data.

Static properties use:: (double colon) self::$property to access

For example, call a member method of a class

<?php
  class SimpleClass{
         public $name="Tome";
         //若不声明public 则默认是public
          $name="Tome";       
   }

   //实例化对象访问属性用->
  $simple=new SimpleClass();
  $simple->name;
?>
Copy after login

PHP’s property or method Access control is achieved by adding the keywords public (public), protected (protected) or private (private) in front.

Public: Public class members can be accessed from anywhere.

protected (protected): Protected class members can be accessed by itself and its subclasses and parent classes.

private (private): Private class members can only be accessed by the class in which they are defined.

Class attributes must be defined as one of public, protected, and private. If defined with var, it is considered public.

The above is the detailed content of What attributes does the php class have?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!