Home php教程 php手册 PHP面向对象(OOP)编程完全教程:4.如何抽象出一个类?

PHP面向对象(OOP)编程完全教程:4.如何抽象出一个类?

Jun 06, 2016 pm 07:58 PM
oop php one how object abstract Tutorial programming For

上面已经介绍过了, 面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,所以我们首先要做的就是如何来声明类, 做出来一个类很容易,只要掌握基本的程序语法定义规则就可以做的出来,那么难点在那里呢? 一个项目要用到多少个类,用多少个对象,

      上面已经介绍过了, 面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,所以我们首先要做的就是如何来声明类, 做出来一个类很容易,只要掌握基本的程序语法定义规则就可以做的出来,那么难点在那里呢? 一个项目要用到多少个类,用多少个对象, 在那要定义类,定义一个什么样的类,这个类实例化出多少个对象, 类里面有多少个属性, 有多少个方法等等,这就需要读者通过在实际的开发中就实际问题分析设计和总结了。 类的定义:

class 类名{
}
Copy after login

使用一个关键字class和后面加上一个你想要的类名以及加上一对大括号, 这样一个类的结构就定义出来了,只要在里面写代码就可以了, 但是里面写什么? 能写什么?怎样写才是一个完整的类呢?上面讲过来,使用类是为了让它实例出对象来给我们用, 这就要知道你想要的是什么样的对象了,像上面我们讲的一个装机配置单上写什么,你装出来的机子就有什么。比如说,一个人就是一个对象,你怎么把一个你看好 的人推荐给你们领导呢?当然是越详细越好了:

首先, 你会介绍这个人姓名、性别、年龄、身高、体重、电话、家庭住址等等。

然后,你要介绍这个人能做什么, 可以开车, 会说英语, 可以使用电脑等等。

只要你介绍多一点, 别人对这个人就多一点了解, 这就是我们对一个人的描述, 现在我们总结一下,所有的对象我们用类去描述都是类似的, 从上面人的描述可以看到, 做出一个类来, 从定义的角度分两部分, 第一是从静态上描述, 第二是从动态上描述, 静态上的描述就是我们所说的属性, 像上面我们看到的,人的姓名、性别、年龄、身高、体重、电话、家庭住址等等。 动态上也就是人的这个对象的功能,比如这个人可以开车, 会说英语, 可以使用电脑等等,抽象成程序时,我们把动态的写成函数或者说是方法,函数和方法是一样的。所以,所有类都是从属性和方法这两方面去写, 属性又叫做这个类的成员属性,方法叫做这个类的成员方法。

class 人{
  成员属性:姓名、性别、年龄、身高、体重、电话、家庭住址
  成员方法:可以开车, 会说英语, 可以使用电脑
}
Copy after login

属性:

通过在类定义中使用关键字" var "来声明变量,即创建了类的属性,虽然在声明成员属性的时候可以给定初值, 但是在声明类的时候给成员属性初始值是没有必要的,比如说要是把人的姓名赋上“张三”,那么用这个类实例出几十个人,这几十个人都叫张三了,所以没有必 要, 我们在实例出对象后给成员属性初始值就可以了。

如: var $somevar;

方法(成员函数):

通过在类定义中声明函数,即创建了类的方法。

如:

<?php class Person
{
    //下面是人的成员属性
    var $name; //人的名子
    var $sex; //人的性别
    var $age; //人的年龄

    //下面是人的成员方法
    function say() { //这个人可以说话的方法
        echo "这个人在说话";
    } 

    function run() { //这个人可以走路的方法
        echo "这个人在走路";
    }
}
?>
Copy after login

上面就是一个类的声明, 从属性和方法上声明出来的一个类, 但是成员属性最好在声明的时候不要给初使的值, 因为我们做的人这个类是一个描述信息, 将来用它实例化对象, 比如实例化出来10个人对象, 那么这10个人, 每一个人的名子, 性别, 年龄都是不一样的, 所以最好不要在这个地方给成员属性赋初值, 而是对每个对象分别赋值的。

用同样的办法可以做出你想要的类了, 只要你能用属性和方法能描述出来的实体都可以定义成类, 去实例化对象。

为了加强你对类的理解, 我们再做一个类, 做一个形状的类, 形状的范围广了点, 我们就做个矩形吧, 先分析一下, 想一想从两方面分析,矩形的属性都有什么? 矩形的功能都有什么?


class 矩形
{
    //矩形的属性
    矩形的长;
    矩形的宽;

    //矩形的方法
    矩形的周长;
    矩形的面积;
}
Copy after login

<?php class Rect
{
    var $kuan;
    var $gao;

    function zhouChang() {
        计算矩形的周长;
    }

    function mianJi() {
        计算矩形的面积;
    }
}
?>
Copy after login

如果用这个类来创建出多个矩形对象,每个矩形对象都有自己的长和宽, 都可以求出自己的周长和面积了。
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

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

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

See all articles