Home Backend Development PHP Tutorial 深入了解PHP类Class的概念_PHP

深入了解PHP类Class的概念_PHP

Jun 01, 2016 pm 12:11 PM
class php

例如,一个交通工具可以定义有颜色、轮胎数、制造商、型号和容量等性质,并定义有停止、前进、转弯和鸣笛等行为。在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 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles