There are three types of PHP members: 1. Member attributes (class attributes), which are variables defined in the class, used to store data in the class; 2. Member methods, which are functions defined in the class , provided to objects to resolve events; 3. Class constants are constants defined in the class and are defined using the const keyword.
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
Class members refer to a series of data defined in the class. They are directly dependent on the first-level members of the class. Class members include: member attributes, member methods, and class constants.
Member attributes (class attributes): variables defined in the class, used to store data in the class
Member methods: that is Functions defined in a class are provided to objects to resolve events.
Class constants: Constants defined in a class are defined using the const keyword.
Properties and methods need to be modified with access qualification modifiers when accessed.
Syntax:
class 类名{ public $成员属性名[=成员属性值]; //成员属性可以只定义而不赋值,放在之后赋值。 [public] function 成员方法名(){ ... } const 常量名=值; }
The member attributes of a class can be defined without assigning values. They can be assigned values in subsequent logical operations according to the situation, but they must be modified with access restriction modifiers and access restrictions for member methods. The modifier can be omitted. If omitted, it defaults to public. The qualification modifier will be explained in detail later.
The steps to implement class members also require 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
Analysis class Whether you want to do something to determine whether you want to define a member method.
Analyze whether constants need to be defined
Object instantiation
Call class members through objects
Example: A simple example of a class for analyzing news submissions
To implement news submissions, you must first declare the class structure:
class devote{ }
Then analyze whether data needs to be stored , it is obvious that data must be stored when submitting news. First, the news title and news content must be stored, and then the contributor's information, including nickname, email address, mobile phone number, etc.
We store these data through member attributes:
class devote{ public $nickname="投稿人"; public $title="新闻标题"; public $content="投稿内容"; public $email="邮箱"; public $phone="手机号"; }
Then analyze whether operations need to be performed. To submit news, you must first filter the news content, block inappropriate words, and then submit it to the database to save the draft. Moderated.
class devote{ public $nickname="投稿人"; public $content="投稿内容"; public $email="邮箱"; public $phone="手机号"; function clear($content){ //过滤新闻内容 } function input(){ //提交到数据库 } }
Do I 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 implement the function.
$devote=new devote(); //实例化对象 $devote->nickname="小杜"; $devote->title="一件大事"; $devote->content="我手机没电了"; $devote->email="2811051813@qq.com"; $devote->phone="就不告诉你"; $devote->clear($devote->content); $devote->input(); //通过调用成员实现业务
Regarding calling class members, we use the -> link. As shown in the above example, the syntax for calling class attributes is:
$Object ->Attribute name;
Please note: There is no need to add the $ sign when calling the attribute name. When calling, they are integrated. But when defining attributes, there must be a $ symbol to indicate defining 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 "news title". If it is assigned a value again, this value will be changed. The value of the title is a big deal. If the attribute is reassigned, the value obtained by using the attribute next will be the content assigned later, because the class attribute is actually a variable.
Reassignment is equivalent to:
$devote->title="一件大事"; //等同于: public $title="一件大事";
Call class method:
$Object->Method name (parameter)
Calling class constants is a static call, which will be explained in detail later.
You can visually display the above content by printing the object:
class devote{ public $nickname="投稿人"; public $content="投稿内容"; public $email="邮箱"; public $phone="手机号"; function clear($content){ //过滤新闻内容 } function input(){ //提交到数据库 } } $devote=new devote(); var_dump($devote); //object(devote)#1 (4) { ["nickname"]=> string(9) "投稿人" ["content"]=> string(12) "投稿内容" ["email"]=> string(6) "邮箱" ["phone"]=> string(9) "手机号" }
Test it yourself, you can see that the value of the member attribute is the default. If the member attribute is reset through the object Assign a value, and then print the object:
$devote->nickname="小杜"; $devote->title="一件大事"; $devote->content="我手机没电了"; $devote->email="2811051813@qq.com"; $devote->phone="就不告诉你"; $devote->clear($devote->content); $devote->input(); var_dump($devote);
You can find that the value of the class attribute has been changed.
The above example has already involved member operations, that is, accessing member attributes and modifying member attributes. The basic operation of element attributes is adding, deleting, modifying and checking. , check and change have been demonstrated above, please see the operation of adding attributes:
To add member attributes, just call a non-existent member attribute directly after the object and assign the value:
$devote->id=1; /* 打印结果: object(devote)#1 (6) { ["nickname"]=> string(6) "小杜" ["content"]=> string(18) "我手机没电了" ["email"]=> string(17) "2811051813@qq.com" ["phone"]=> string(15) "就不告诉你" ["title"]=> string(12) "一件大事" ["id"]=> int(1) } */
Execute the above After the operation, 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; //输出1
Directly calling a non-existent attribute will not report an error, but he The value is NULL.
var_dump($devote->ids); //NULL
Use the unset method to delete class attributes:
unset($devote->id); var_dump($devote->id); //NULL
Class attributes operated through objects only take effect under the current object:
$devote->age=18; $dv=new devote(); var_dump($dv); //object(devote)#2 (4) { ["nickname"]=> string(9) "投稿人" ["content"]=> string(12) "投稿内容" ["email"]=> string(6) "邮箱" ["phone"]=> string(9) "手机号" }
Method operations can only call one operation, that is Member methods of a class can only be called through objects:
$devote->input();
Note: Class members can be defined unlimitedly in a class, but a class can only contain class attributes, class methods, and class constants. All other syntaxes are It cannot be written in a class.
class Test{ echo 1234; var_dump("错误"); if($a==1){ echo "True"; } } //syntax error, unexpected 'echo' (T_ECHO), expecting function (T_FUNCTION) or const (T_CONST)
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of There are several types of php members. For more information, please follow other related articles on the PHP Chinese website!