What attributes are in php members

03-28-2023

This article mainly explains what attributes are in php members. The content of the explanation in the article is simple and clear, easy to learn and understand. Next, please follow the editor's ideas and slowly deepen, and let's study and learn what are in php members. Property it!


There are three types of PHP members: 1. Member attributes (class attributes), that is, variables defined in the class, used to store data in the class; 2. , Member methods, that is, functions defined in the class, provided to the object to resolve events; 3, Class constants, which are constants defined in the class, defined using the const keyword.

1. Concept

A class member refers to a series of data defined in a class, they are first-level members directly dependent on the class . Class members include: member attributes, member methods, and class constants.

  • Member attribute (class attribute): the variable defined in the class is used to store the data in the class

  • member Method: The function defined in the class is provided to the object to resolve the event.

  • Class constants: Constants defined in a class are defined using the const keyword.

Properties and methods need to be modified with access restriction modifiers when accessing.

Syntax:


class class name{ the 

Public $member attribute name[=member attribute value]; //The member attribute can only be defined without assignment, and assigned later. the 

[public] function member method name(){                     ...            }    const constant name = value;  the }


The member attributes of the class can only be defined without assignment, and assigned according to the situation in subsequent logical operations, but must be decorated with access restriction modifiers, access to member methods The limit modifier can be omitted. After omitting, it will be public by default. The limit modifier will be explained in detail later.

Second, implementation steps

The steps to implement class members also need to carry out business logic analysis, the general steps are:

  • Declare class structure

  • Analyze whether the class needs to store data to determine whether to define member attributes or how many member attributes to define

  • Analyze whether the class needs to perform certain operations to determine whether to define member methods.

  • Analysis is neededDefining constants

  • Object instantiation

  • Calling class members through objects

Example: A simple example of a class that analyzes news submissions

To implement news submissions, first declare the class structure:


class devote{ }


Then analyze whether the data needs to be stored. Obviously, the submitted news must store the data. First, the news title and news content must be stored, and then the contributor’s information, including nickname, email address, mobile phone number and so on.

We store these data through member attributes:


class devote{ 

Public $nickname="Contributor"; 

Public $title="News Title"; 

Public $content="Contribution content"; 

Public $email="mailbox";  

public $phone="mobile phone number";  }


Then analyze whether it is necessary to perform an operation. To submit news, you must first filter the news content, block inappropriate words, and then submit it to the database to save the draft and wait for review.


class devote{ 

Public $nickname="Contributor"; 

Public $content="Contribution content"; 

Public $email="mailbox"; 

Public $phone="Mobile phone number"; 

function clear($content){ //Filter news content } 

function input(){ //submit to database     }   }


Do you need class constants? It is not needed for the time being, then the next step is to instantiate the class, and then call the members through the object to realize the function.


$devote=new devote(); 

// instantiate object 

$devote->nickname="Xiao Du"; 

$devote->title="A big event";  

$devote->content="My phone is out of battery"; 

$devote->email="[email protected]";the 

$devote->phone="I won't tell you";  

$devote->clear($devote->content); 

$devote->input(); 

//Realize business by calling members


Regarding the call of class members, we use the -> link, as shown in the above example, the syntax of calling class attributes is:

$object->property name;

Please note: there is no need to add a $ character to call the property name, when calling, they are integrated. But when defining attributes, there must be a $ character to indicate the definition of variables.

Calling the attribute will get the value of the current attribute, and assigning a value to it will change its value. As above, if the title attribute is called directly, the value obtained is the news title. If you assign a value to it again, at this time The value of title is a big deal. If the attribute is reassigned, the value obtained by using this attribute in the future will be the content of the later assignment, because the class attribute is actually a variable.

Reassignment is equivalent to:


$devote->title="A big event";  //Equivalent to: public $title="A big event";


Call class method:

$object->method name (parameter)

The call class constant is a static call, which will be explained in detail later.

The above content can be displayed intuitively by printing the object:


class devote{ 

Public $nickname="Contributor";   

public $content="Contribution content";   

public $email="email";   

public $phone="mobile phone number";  

function clear($content){ 

//Filter news content     }   

function input(){ 

//submit to database     }   } the 

$devote=new devote(); 

var_dump($devote); 

//object(devote)#1 (4) { ["nickname"]=> string(9) "Contributor" ["content"]=> string(12) "Contribution content" ["email"]=> string(6) "Mailbox" ["phone"]=> string(9) "Mobile phone number" }


Test it yourself, you can see the member attributes The value is the default. If you reassign the member attribute through the object, and then print the object:


$devote->nickname="Xiaodu";  

$devote->title="A big event";  

$devote->content="My phone is out of battery"; 

$devote->email="[email protected]"; 

$devote->phone="I won't tell you";  

$devote->clear($devote->content); 

$devote->input(); 

var_dump($devote);


You can find that the value of the class attribute has been changed.

3. Member operations

The above examples have involved member operations, that is, accessing member attributes and modifying member attributes. The basic operations of element attributes are Add, delete, modify, check, check and modify have been demonstrated above, please see the operation of adding attributes:

Adding a member attribute directly calls a non-existing member attribute after the object and assigns a value:


$devote->id=1;  /* Print result: object(devote)#1 (6) { ["nickname"]=> string(6) "Xiaodu" ["content"]=> string(18) "My phone is out of battery" ["email" ]=> string(17) "[email protected]" ["phone"]=> string(15) "I won't tell you" ["title"]=> string(12) "A big event" ["id "] => int(1) } */


After performing the above operations, a new attribute id will be added to the class and assigned a value of 1. This value can also be directly accessed through the object later:


echo $devote->id; //Output 1


Directly calling an attribute that does not exist will not report an error, but His value is NULL.


var_dump($devote->ids); //NULL


Delete class attributes using the unset method:


unset($devote->id);  var_dump($devote->id); //NULL


The class attribute operated by the object only takes effect under the current object:


$devote->age= 18; $dv=new devote(); var_dump($dv); //object(devote)#2 (4) { ["nickname"]=> string(9) "Contributor" ["content"]=> string(12) "Contribution content" ["email"]=> string (6) "Mailbox" ["phone"]=> string(9) "Mobile phone number" }


The method operation can only call one operation, that is, only through the object Call the member method of the class:


$devote->input();


Note: class members It can be defined infinitely in the class, but the class can only contain class attributes, class methods, and class constants, and other syntax cannot be written in the class.


class Test{ 

Echo 1234; 

var_dump("error"); 

if($a==1){ echo "True"; } } 

//syntax error, unexpected 'echo' (T_ECHO), expecting function (T_FUNCTION) or const (T_CONST)



Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us