<?php Machine Soldier Class Class Marine { The user ID to which it belongs Public $playerID constructor, parameter is the user's ID Public function __construct ($id) { $this->playerid = $id; } } The management of the class Class Troopmanager { arrays, for storing prototypes of multiple classes Public $troopPrototype = Array (); Add the prototype, the first parameter is the prototype name, the second parameter is the prototype object Public Function Addprototype ($name, $prototype) { $this->troopprototype[$name] = $prototype; } Get the clone of the prototype, which is the alternative to the new method, the parameter is the prototype name Public Function Getprototype ($name) { return clone $this->troopprototype[$name]; } } Initialize the class management $manager = new Troopmanager (); Initialize two of different player's prototypes $m 1 = new Marine (1); $m 2 = new Marine (2); Adding prototypes and naming prototypes with more easily memorized names $manager->addprototype (' Marine of 1 ', $m 1); $manager->addprototype (' Marine of 2 ', $m 2); When new objects are needed, we don't have to know the object's class name and initialization work $m 3 = $manager->getprototype (' Marine of 1 '); ?> |