What is the way to write php class

藏色散人
Release: 2023-03-13 16:36:02
Original
2261 people have browsed it

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) {...}}".

What is the way to write php class

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:

Copy after login

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

url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } ?>
Copy after login

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!

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
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!