本文介绍了什么是PHP中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我从一本书中理解PHP类有严重的问题。他们似乎很难。他们的目的是什么,以及他们如何工作?解决方案 一个对象的蓝图。一个对象封装了概念上相关的状态和责任在你的应用程序的东西,通常提供一个编程接口与之进行交互。 想象一个锁: 命名空间MyExample; class Lock { private $ isLocked = false; public function unlock() { $ this-> isLocked = false; echo'你解锁了锁'; } public function lock() { $ this-> isLocked = true; echo'你锁定了锁'; } public function isLocked() { return $ this-> isLocked; } } 忽略命名空间, private 和 public Lock类是应用程序中所有Locks的蓝图。可以通过属性 $ isLocked 表示锁定锁定或解锁。由于它只能有这两个状态,我使用布尔值( true 或 false )来指示哪个状态适用。我可以通过方法 lock 和 unlock 与锁互动,这将更改状态。 isLocked 方法将给我的锁的当前状态。现在,当您从此蓝图创建一个对象(通常也称为实例)时,它将封装唯一状态,例如 $ aLock = new Lock; //从类蓝图创建对象 $ aLock-> unlock(); //你解锁了锁 $ aLock-> lock(); //锁定锁 让我们创建另一个锁,还封装自己的状态 $ anotherLock = new Lock; $ anotherLock-> unlock(); //你解锁了锁 但是因为每个对象/实例封装了它自己的状态,锁定 var_dump($ aLock-> isLocked // give Boolean true var_dump($ anotherLock-> isLocked()); //给出布尔值false 现在,保持锁定被锁定或解锁的整个责任被包含在锁类。你不必每次要锁定的东西重建它,如果你想改变一个锁的工作原理,你可以改变这个在锁定的蓝图,而不是所有的类有 a锁,例如a门: class Door { private $ lock; private $ connectingTo; public function __construct(Lock $ lock) { $ this-> lock = $ lock; $ this-> connectingTo ='bedroom'; } public function open() { if($ this-> lock-> isLocked()){ echo'它被锁定。 } else { echo'你打开了门连接到:',$ this-> connectingTo; } } } 对象,您可以为其分配一个Lock对象。由于Lock对象负责处理某事是否被锁定或解锁的所有责任,所以门不必关心这一点。事实上,任何可以使用Lock的对象都不用关心,例如一个Chest class Chest { private $ lock; private $ loot; public function __construct(Lock $ lock) { $ this-> lock = $ lock; $ this-> loot ='Tons of pieces of Eight'; } public function getLoot() { if($ this-> lock-> isLocked()){ echo'胸部被锁住。 } else { echo'你抢了胸部,得到:',$ this-> loot; } } } 胸部的责任与门的不一致。一个胸部包含战利品,而一个门分隔房间。您可以将锁定或解锁状态编码到这两个类中,但是使用单独的Lock类,您不必重复使用该锁。 $ doorLock = new Lock; $ myDoor = new Door($ doorLock); $ chestLock = new Lock; $ myChest new胸部($ chestLock); 胸部和门现在有独特的锁。如果锁是一种可以同时存在于多个地方的魔法锁,例如在量子物理中,你可以为胸部和门分配相同的锁,例如 $ quantumLock = new Lock; $ myDoor = new Door($ quantumLock); $ myChest new Chest($ quantumLock); 当您 unlock() code> $ quantumLock ,门和胸部都会被解锁。 虽然我承认昆腾锁是一个坏的例子,它说明了共享对象的概念,而不是重建状态和责任在整个地方。一个现实世界的例子可能是一个数据库对象,你传递给类使用数据库。 注意上面的例子不显示如何使用 lock()和 unlock()方法获取到锁的门或门。 此外,请检查何时使用self over $ this?更深入地解释类和对象以及如何使用它们 对于其他资源,请检查 http://en.wikipedia.org/wiki/Object-oriented_programming http://www.php.net/manual/en/language.oop5.php http://www.tuxradar.com/practicalphp http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html http://articles.sitepoint.com/article/object-oriented-php ul> I'm having serious issues understanding PHP classes from a book. They seem very difficult. What is their purpose and how do they work? 解决方案 In a nutshell, a Class is a blueprint for an object. And an object encapsulates conceptually related State and Responsibility of something in your Application and usually offers an programming interface with which to interact with these. This fosters code reuse and improves maintainability.Imagine a Lock:namespace MyExample;class Lock{ private $isLocked = false; public function unlock() { $this->isLocked = false; echo 'You unlocked the Lock'; } public function lock() { $this->isLocked = true; echo 'You locked the Lock'; } public function isLocked() { return $this->isLocked; }}Ignore the namespace, private and public declaration right now.The Lock class is a blueprint for all the Locks in your application. A Lock can either be locked or unlocked, represented by the property $isLocked. Since it can have only these two states, I use a Boolean (true or false) to indicate which state applies. I can interact with the Lock through it's methods lock and unlock, which will change the state accordingly. The isLocked method will give me the current state of the Lock. Now, when you create an object (also often referred to as an instance) from this blueprint, it will encapsulate unique state, e.g.$aLock = new Lock; // Create object from the class blueprint$aLock->unlock(); // You unlocked the Lock$aLock->lock(); // You locked the LockLet's create another lock, also encapsulating it's own state$anotherLock = new Lock;$anotherLock->unlock(); // You unlocked the Lockbut because each object/instance encapsulates it's own state, the first lock stays lockedvar_dump( $aLock->isLocked() ); // gives Boolean truevar_dump( $anotherLock->isLocked() ); // gives Boolean falseNow the entire reponsibility of keeping a Lock either locked or unlocked is encaspulated within the Lock class. You don't have to rebuilt it each time you want to lock something and if you want to change how a Lock works you can change this in the blueprint of Lock instead of all the classes having a Lock, e.g. a Door:class Door{ private $lock; private $connectsTo; public function __construct(Lock $lock) { $this->lock = $lock; $this->connectsTo = 'bedroom'; } public function open() { if($this->lock->isLocked()) { echo 'Cannot open Door. It is locked.'; } else { echo 'You opened the Door connecting to: ', $this->connectsTo; } }}Now when you create a Door object you can assign a Lock object to it. Since the Lock object handles all the responsibility of whether something is locked or unlocked, the Door does not have to care about this. In fact any objects that can use a Lock would not have to care, for instance a Chestclass Chest{ private $lock; private $loot; public function __construct(Lock $lock) { $this->lock = $lock; $this->loot = 'Tons of Pieces of Eight'; } public function getLoot() { if($this->lock->isLocked()) { echo 'Cannot get Loot. The chest is locked.'; } else { echo 'You looted the chest and got:', $this->loot; } }}As you can see, the reponsibility of the Chest is different from that of a door. A chest contains loot, while a door separates rooms. You could code the locked or unlocked state into both classes, but with a separate Lock class, you don't have to and can reuse the Lock.$doorLock = new Lock;$myDoor = new Door($doorLock);$chestLock = new Lock;$myChest new Chest($chestLock);Chest and Door now have their unique locks. If the lock was a magical lock that can exist in multiple places at the same time, like in Quantum physics, you could assign the same lock to both chest and door, e.g.$quantumLock = new Lock;$myDoor = new Door($quantumLock);$myChest new Chest($quantumLock);and when you unlock() the $quantumLock, both Door and Chest would be unlocked.While I admit Quantum Locks are a bad example, it illustrates the concept of sharing of objects instead of rebuilding state and responsibility all over the place. A real world example could be a database object that you pass to classes using the database.Note that the examples above do not show how to get to the Lock of a Chest or a Door to use the lock() and unlock() methods. I leave this as an exercise for your to work out (or someone else to add).Also check When to use self over $this? for a more in-depth explanation of Classes and Objects and how to work with themFor some additional resources checkhttp://en.wikipedia.org/wiki/Object-oriented_programminghttp://www.php.net/manual/en/language.oop5.phphttp://www.tuxradar.com/practicalphphttp://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.htmlhttp://articles.sitepoint.com/article/object-oriented-php 这篇关于什么是PHP中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!