Detailed introduction to the definition of php classes and methods

王林
Release: 2023-02-23 14:24:01
Original
5176 people have browsed it

Detailed introduction to the definition of php classes and methods

In PHP, classes and methods are necessary and must be mastered. They are also the basis for the subsequent development process, so it is very important to deeply learn and master PHP classes and methods. . This article explains in detail the definition of PHP classes and methods and matters needing attention for beginners.

Definition of class:

A class is the definition of an object. It contains information about how the object behaves, including its name, methods, properties, and events. It's not actually an object itself because it doesn't exist in memory. When the code that references the class runs, a new instance of the class, an object, is created in memory. Although there is only one class, multiple objects of the same type can be created in memory from this class. Classes are defined through the class keyword.

Basic syntax:

class 类名{
   //属性、方法
   }
Copy after login

1. Define a class (can only be defined with class)

2. Define a class The attribute public (public is a modifier, there are three in total, they are public, protected, private, here we only use public for now)

For example:

Define a car class, the attributes are The color and price of the car

class car{

  public $color;     //定义属性

  public $price;

}
Copy after login

Definition of method (function):

(1) Regular function: Use the function keyword to define a function without specifying a return Value type

For example:

function test(){
//函数体
}
Copy after login

(2) Internal function: cannot be called directly, only when the external function is called first can it be called:

For example:

function demo()
{
       function fun1()
       {
        echo "aaaa";
        }
       function fun2()
        {
         echo "bbb";
        }
}
fun1();//获取不到这个函数
fun2();//获取不到这个函数
 
function demo()
{
       function fun1()
       {
        echo "aaaa";
        }
       function fun2()
        {
         echo "bbbb";
        }
}
demo();
fun1();//得到的结果为aaaa
fun2();//得到的结果为bbbb
Copy after login

(3) Variable function

Definition: If there are brackets after a variable, for example: $var=hello; use $var(); then the program will look for a variable with the same name as the value Function

For example:

$var='hello';
function hello(){
echo "aaaa";
}
$var();//将会去执行hello()函数
Copy after login

Note: Do not add a semicolon after () and {}.

Recommended video tutorial: PHP video tutorial

The above is the detailed content of Detailed introduction to the definition of php classes and methods. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template