【问题标题】:PHP 'sibling' classes? How does it work?PHP“兄弟”类?它是如何工作的?
【发布时间】:2012-08-02 09:05:35
【问题描述】:

我知道这是一直在做的,但出于某种原因,这对我来说一点意义都没有。 [插入@#$%!这里]我不知道这个(假设)明显的解决方案的正确 OOP 过程是什么。我已经简要阅读了 Abstract 和 Interface 类型类,但对任何符合我要求的示例均无济于事。

所以这里是设置...

在此示例中,我有两个类,一个将从我的脚本中调用,另外两个可以从我的脚本中调用,也可以从我调用的第一个类中调用。伪代码:

<?php

    // Some code here

    $timer = new timer();
    $doSomethingCool = new doSomethingCool();

    $doSomethingCool->doIt();
    print $timer->length();



    class timer {
        private $startTime;
        private $stopTime;
        private $length;

        public function start() {
            // Start running the timer
        }
        public function end() {
            // End the timer
        }
        public function length() {
            // Return the length of the timer
            return $this->length;
        }
    }

    class doSomethingCool {
        public function doIt() {

            $timer->start();

            // Some code here

            $timer->end();

        }
    }

?>

我已经能够让它“运行”(见下文),但这种解决方法很混乱,我 100% 确定这不是正确的面向对象建模:

<?php
    // Start by declaring all classes and pass all classes to themselves...

    $doSomethingCool = new doSomethingCool();
    $timer = new timer();
    $class = array(
        'doSomethingCool'=>$doSomethingCool,
        'timer'=>$timer
    );

    $doSomethingCool->class = $class;
    $timer->class = $class;


    // Some code here
    $class['doSomethingCool']->doIt();
    print $timer->length();



    class timer {
        // In each class we now declare a public variable
        // 'class' that we had passed all class instances to...
        public $class;

        private $startTime;
        private $stopTime;
        private $length;

        public function start() {
            // Start running the timer
        }
        public function end() {
            // End the timer
        }
        public function length() {
            // Return the length of the timer
            return $this->length;
        }
    }

    class doSomethingCool {
        public $class;

        public function doIt() {

            $this->class['timer']->start();

            // Some code here

            $this->class['timer']->end();

        }
    }

?>

由于 E_STRICT,我不想使用$timer::start();。

那么,女士们,先生们,解决方案是什么?谢谢!

【问题讨论】:

  • 我不明白这个问题。 究竟你想用这个来完成什么? (有时英文比代码更容易阅读:))
  • 您的伪代码有一个全局变量$timer,您正试图将其用作局部变量。在本地创建 $timer 或使用 global $timer;。
  • @minitech,哈,我听到了...所以基本上,我希望能够从另一个单独的类函数中调用一个类函数。
  • @Vatev,让我确保我没有弄乱我的假... 编辑:你是对的,如果我愿意,我可以这样做。不过,这对我来说似乎不是正确的编码。
  • 当你说assumingly时,你的意思是presumably吗?

标签: php oop class interface


【解决方案1】:

如果您希望 A 类的对象 a 访问 B 类的对象 b 则必须将其传递给 a 如果 b 在某种意义上不是全局的。在你的情况下,你可以这样做:

  • 通过将其添加到构造函数new doSomethingCool($timer) 或
  • 通过创建和使用 set 方法并将对象句柄存储为私有属性,例如。 $doSomethingCool-&gt;setTimer( $timer ); 或
  • 通过在doIt() 中作为参数传递

另一种选择是对计时器类使用单例模板——这实际上使计时器成为一个 glbal 对象,但现在通常不推荐使用此类模板。

【讨论】:

  • @J-Law 或者如果您愿意,您可以将 timer 实例传递给 doIt()。
  • @JMM,你是对的,你可以将它传递给 doIt 方法,但我的意思是计时器是全局的(yuk)或者你必须传递它。我会澄清我的答案。
  • 是的,同意,只是为了 OP 而建立在您的答案之上。因为他们一开始不明白这一点,所以我不认为他们能够将您的答案中的各个点联系起来以意识到他们可以将其传递给doIt()。
  • 为了一个好的答案,我会放弃那个单身废话;-)
  • @JMM,您的想法最大的问题是我必须拥有同一个类的单独实例......当我想共享变量时效果不佳。
【解决方案2】:

我认为您想将其他类注入某个类。如果是这样:

class Database {}
class Timer {}

class Foo
{
    protected $database;
    protected $timer;

    public function __construct(Database $database, Timer $timer)
    {
         $this->database = $database;
         $this->timer = $timer;
    }

    public function doSomething()
    {
        $this->timer->start();
    }

}

$database = new Database();
$timer = new Timer();
$foo = new Foo($database, $timer);

请注意,您当然应该添加您的真实课程(我使用了Database 和Timer)。

【讨论】:

    【解决方案3】:

    多态方式:

    class SomeClass {
        protected $db;
        function __construct(Database $database) {
            $this->db = $database;
        }
        function doIt() {
            // . . .
        }
    }
    
    class TimedSomeClass extends SomeClass {
        protected $timer;
        function __construct(Database $db, Timer $timer) {
            $this->timer = $timer;
            parent::__construct($db);
        }
    
        function doIt() {
            $this->timer->start();
            parent::doIt();
            $this->timer->stop();
        }
    }
    

    多态可能不是解决这种情况的最佳工具,但听起来你想在你的问题中多态地做它,所以就在这里。

    【讨论】:

      猜你喜欢
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多