星际争霸之php面向对象(一)
题记
这是一部学习php设计模式的专集,作者以经典的游戏-星际争霸洋洋洒洒地讨论PHP面向对象和php的设计模式,内容比较全面,网志博客觉得它称得上一部学习php设计模式的经典之作,本来网志博客还打算以网页游戏武林三国来写一个php设计模式专辑的,但看到星际争霸的php设计模式后,我打消了这个念头了。
本php设计模式专辑来源于博客(jymoz.com),不过后来博客被封(2012年)了,又因为作者jymoz没有备份,以致先前很多原创博文包括我接下来要分享的php设计模式专集都消失了,幸好两年前我收藏了作者php设计模式的全部博文,感谢作者jymoz的辛苦付出哦!
==============================================================================
前言
面向对象博大精深,对于从未接触过得的人,会觉得一头雾水。
学习的资料很多,但大多比较抽象,所以我用经典的游戏-星际争霸来讨论PHP面向对象。
现在假设我们来用PHP开发星际争霸,从而接触PHP面向对象。
注意,为了便于学习,除了特殊说明,否则各部分代码之间没有关联。而且同一件事情往往用的是不同的代码。
另外我也不去考证各个兵种的属性数字,仅仅用来说明。
一、类和对象
如果玩家制造了一个机枪兵,那么我们怎么表示他呢,因为每个机枪兵有几个基本的数据要记录:剩余的血,杀敌数量,攻击力等等。
我们可以用一个数组来记录一个机枪兵剩余的血和杀敌数量,因为这对于每个机枪兵是独立的。
但攻击力比较麻烦,因为经过升级,攻击力会增加,这就必须要找出所有表示机枪兵的数组,然后进行修改,非常麻烦。
从这里我们可以看出一件事情,首先每个机枪兵有独立的数据需要记录和修改,比如剩余的血。同时他们有相同的数据需要共用,比如攻击力。
这时候面向对象就能帮上我们的忙了。
1.1、类的定义
我们先来处理一部分问题,也就是每个机枪兵独有的数据。
class marine { public $blood = 50; //剩余的血 public $kills = 0; //杀敌数量 //这个函数(通常叫做方法)表示攻击敌人时候的运行代码 function attack($enemy) { //攻击敌人的代码 } }
这叫做类,我们建立了一个表示所有机枪兵的类marine,这里面保留了需要每个兵独有的数据,比如上面代码里的剩余的血。
1.2、对象的创建和使用
接下来我们来使用对象,也就是每个机枪兵:
$m1 = new marine();
通过new后面加一个类的名字和括号,我们新建了一个机枪兵$m1,$m1被叫做类marine的对象,我们可以把它想象成一个特殊变量,只不过里面保存了多个数据。
如果需要使用或者操作某个机枪兵的血(对象的属性),只要用$m1->blood来表示就可以了:
echo $m1->blood;//输出机枪兵$m1剩余的血 我们再建立一个机枪兵 $m2 = new marine(); 如果此时$m1被敌人攻击过了,还剩下10个血。而$m2没受过攻击: echo $m1->blood;//结果是10 echo $m2->blood;//结果是50
使用对象可以很简单的保存每个机枪兵的血,不会互相影响。
如果机枪兵$m1攻击敌人的时候,可以这样使用对象的方法:
$m1->attack($z1);//假设攻击的是某个小狗的对象$z1
不同的类内可以用同名的函数,比如小狗的类Zergling里面也可以有一个函数attack
要注意的是,从PHP5开始,无论在哪里改变一个对象的属性,都能改变它。比如上面一个小狗对象被作为参数传入机枪兵的attack函数,执行函数之后这个小狗对象的血减少了,这和一般的函数不同。但这是很直观的,如果一个小狗被攻击了,它的血就应该减少。
二、构造函数和析构函数
每次我们新建一个机枪兵的时候,总人口应该加1,如果一个机枪兵被杀,人口应该减少1。
可以通过构造函数和析构函数来自动处理:
class marine { //构造函数 function

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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,

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

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.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
