Home  >  Article  >  Backend Development  >  How to implement classes in php

How to implement classes in php

王林
王林Original
2020-11-06 10:02:522223browse

The method for php to implement a class is: [class class name { }], such as [

How to implement classes in php

Implementing classes and objects in PHP

(Learning video recommendation: java course)

1. Create a class

Syntax:

class 类名{
	//属性
	//方法
	//常量
}
类是由属性、方法、常量组成的,也可以说
类成员有:属性、方法、常量

Naming rules for class names:

starts with letters and underscores, followed by letters, numbers, and underscores

Cannot use PHP keywords as class names

Class names are not case-sensitive (variable names are case-sensitive, keywords and class names are not case-sensitive)

Use Pascal nomenclature for class names (The first letter of a word in camel case is capitalized)

<?php
class Student {
}

2. Object instantiation

Instantiate an object through the new keyword.

<?php
//定义类
class Student {
	
}
//实例化对象
$stu1=new Student();
$stu2=new Student;		//小括号可以省略
var_dump($stu1,$stu2);	//object(Student)#1 (0) { } object(Student)#2 (0) { }

Related recommendations: php training

The above is the detailed content of How to implement classes in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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