PHP>如何将一个Class划分为多个Class?

发布于 2024-12-13 07:28:22 字数 686 浏览 2 评论 0原文

我有一个非常大的 PHP 类,名为“Player”。

里面有很多函数(近2000行)。实际上,我以这种方式使用这些函数:

$player = new Player(1)
echo $player->getLogin();
echo $player->getStatisticPerception();
echo $player->getWeaponId();
echo $player->attackPlayerId(3,$player->getWeaponId());

我认为将此类划分为多个类可能是一个好主意,但我不知道如何划分。我怎样才能创建类似的东西,例如:

$player = new Player(1);
echo $player->getLogin();
echo $player->statistics->getAttack();
echo $player->stuff->getWeaponId();
echo $player->doAction->attackPlayerId(3, $player->getWeaponId());

如果我认为我必须在这个对象内创建一个对象,但如果我这样做,我将无法访问主“$player”的对象数据(例如,在Stuff中)对象,我无法访问玩家对象的 $level 变量。)

I have a very big PHP class called "Player".

There is a lot of functions inside it(almost 2000 lines). Actually, I use those functions in this way :

$player = new Player(1)
echo $player->getLogin();
echo $player->getStatisticPerception();
echo $player->getWeaponId();
echo $player->attackPlayerId(3,$player->getWeaponId());

I think it could be a good idea to divide this class into multiples classes, but I don't know how. How could I create something like, for example :

$player = new Player(1);
echo $player->getLogin();
echo $player->statistics->getAttack();
echo $player->stuff->getWeaponId();
echo $player->doAction->attackPlayerId(3, $player->getWeaponId());

If think I have to create an object inside this object, but if I do so, i can't access the main "$player"'s object data (for example, in the Stuff Object, I can't access on the $level variable of the Player Object.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场信仰旅途 2024-12-20 07:28:22

您可以创建多个类,例如 PlayerStatisticsWeaponPlayerActions 并将它们链接到 Player 类...一个示例:

class Player{
    private Statistics; 
    private Weapon;

    function __construct(){
        $this->Statistics = new Statistics($this);
        $this->Weapon = new Weapon($this);
    }

    function getAttack(){
        return $this->Statistics->getAttack();
    }
}

class Statistics{
    private Player;
    function __construct($_player){
         $this->Player = $_player;
    }
}

类似的东西..它是一个对象组合或聚合,具体取决于对象之间的关系。

希望这有帮助

you can create multiple clases like PlayerStatistics, Weapon and PlayerActions and link them to the Player Class... an example:

class Player{
    private Statistics; 
    private Weapon;

    function __construct(){
        $this->Statistics = new Statistics($this);
        $this->Weapon = new Weapon($this);
    }

    function getAttack(){
        return $this->Statistics->getAttack();
    }
}

class Statistics{
    private Player;
    function __construct($_player){
         $this->Player = $_player;
    }
}

something like that... it's an object composition or aggregation, depending on the relation between objects.

Hope this helps

心安伴我暖 2024-12-20 07:28:22

您可以使用类继承

Class Player {
  protected $level;
  public $stuff;
  function __construct() {
    $this->stuff = new Stuff();
  }
}

Class Stuff extends Player {
  function getWeaponId() {
    // You can access Player's $level here using $this->level
  }
}

You can use class inheritance

Class Player {
  protected $level;
  public $stuff;
  function __construct() {
    $this->stuff = new Stuff();
  }
}

Class Stuff extends Player {
  function getWeaponId() {
    // You can access Player's $level here using $this->level
  }
}
耶耶耶 2024-12-20 07:28:22

在这样的行中:

echo $player->statistics->getAttack();

您正在访问 Player 类的 statistics 成员。为了使用链接,该变量也必须是一个对象。您可以创建一个单独的统计类,其中包含 getAttack 方法。然后在 Player 类构造函数中,将 $this->statistics 初始化为统计类的实例。

类似地,对于 stuffdoAction,它们需要是对象。尽管我不确定它们是否真的适合单独对象的候选者。

In a line like this:

echo $player->statistics->getAttack();

You are accessing the statistics member of the Player class. In order to use chaining, that variable must also be an object. You can create a separate Statistics class that includes a getAttack method. Then in the Player class constructor, initialise $this->statistics to an instance of the Statistics class.

Similarly for stuff and doAction, they would need to be objects. Although I'm not sure they are really appropriate candidates for separate objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文