Home php教程 php手册 深入了解PHP类Class的概念

深入了解PHP类Class的概念

Jun 13, 2016 am 11:59 AM
class php learn Transportation manufacturer Can and model definition number concept go deep of kind tire

例如,一个交通工具可以定义有颜色、轮胎数、制造商、型号和容量等性质,并定义有停止、前进、转弯和鸣笛等行为。在OOP术语中,实体的性质和行为的具体定义称为类(class)。

类的定义与创建
类是具有相同属性和服务的一组对象的集合。它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和方法两个主要部分。在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属性说明和方法说明两个主要部分。

类用于表示要在应用程序中处理的实际事物。例如,假设要创建一个管理公共图书馆的应用程序,可能就要包括一些类来表示书籍、杂志、员工、特殊事件、顾客以及需要管理的其他事物。每个实体都包含一组性质和行为,在OOP中分别称为字段(field)和方法(method),它们定义了实体。PHP 中一般的类创建语法如下:

复制代码 代码如下:


class Class_Name
{
// 字段声明
// 方法声明
}


创建一个类:

复制代码 代码如下:


class Employee
{
private $name;
private $title;
protected $wage;

protected function clockIn() {
echo "Member $this->name clocked in at ".date("h:i:s");
}

protected function clockOut() {
echo "Member $this->name clocked out at ".date("h:i:s");
}
}


这个类名为Employee,定义了3个字段:name、title和wage,还定义了两个方法:clockIn(签到)和clockOut(签离)。

类的应用
一个定义了属性和方法的类就是一个完整的类了,可以在一个类里面包含一个完整的处理逻辑。使用 new 关键字来实例化一个对象以便应用类里面的逻辑。可以同时实例化多个对象。

类的实例化:

复制代码 代码如下:


object = new class_name();


实例化一个对象后,使用 -> 操作符来访问对象的成员属性和方法。比如:

复制代码 代码如下:


object->var_name;
object->function_name;


如果要在定义的类里面访问成员的属性或者方法,可以使用伪变量 $this 。$this 用于表示当前对象或对象本身 。

复制代码 代码如下:


class Person {
// 人的成员属性
var $name; //人的名字
var $age; //人的年龄

//人的成员 say() 方法
function say() {
echo "我的名字叫:".$this->name."
";
echo "我的年龄是:".$this->age;
}
}
//类定义结束

$p1 = new Person(); //实例化一个对象
$p1->name = "Gonn"; //给 $p1 对象属性赋值
$p1->age = 25;
$p1->say(); //调用对象中的 say()方法
?>


程序运行结果:

复制代码 代码如下:


我的名字叫:Gonn
我的年龄是:25

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Mar 06, 2025 pm 04:36 PM

Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking)

CakePHP Useful Resources CakePHP Useful Resources Sep 10, 2024 pm 05:27 PM

CakePHP Useful Resources

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

PHP Program to Count Vowels in a String

See all articles