Home  >  Article  >  Backend Development  >  What attributes does the php class have?

What attributes does the php class have?

silencement
silencementOriginal
2019-09-28 11:25:193313browse

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;
?>

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!

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

Related articles

See more