How to implement classes in php

王林
Release: 2023-03-07 12:20:01
Original
2242 people have browsed it

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 类名{
	//属性
	//方法
	//常量
}
类是由属性、方法、常量组成的,也可以说
类成员有:属性、方法、常量
Copy after login

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 {
}
Copy after login

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) { }
Copy after login

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!

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