Home > Article > Backend Development > What is the way to write php class
In PHP, a class defines the abstract characteristics of a thing, including the form of data and operations on data. The syntax format for defining a class is such as "class phpClass {var $var1; function myfunc ($arg1, $ arg2) {...}}".
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
What is the way to write the php class?
Class − Defines the abstract characteristics of a thing. The definition of a class includes the form of the data and the operations on the data.
PHP class definition
PHP definition class usually has the following syntax format:
<?php class phpClass { var $var1; var $var2 = "constant string"; function myfunc ($arg1, $arg2) { [..] } [..] } ?>
The analysis is as follows:
Classes use the class keyword Add the class name definition after.
Variables and methods can be defined within a pair of braces ({}) after the class name.
Variables of the class are declared using var, and variables can also be initialized.
Function definition is similar to the definition of PHP function, but the function can only be accessed through the class and its instantiated objects.
Instance
<?php class Site { /* 成员变量 */ var $url; var $title; /* 成员函数 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } ?>
Variable $this represents the object of itself.
PHP_EOL is the newline character.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the way to write php class. For more information, please follow other related articles on the PHP Chinese website!