代码之家  ›  专栏  ›  技术社区  ›  Kristoffer Bohmann

OOP/PHP5:从B类调用A类-或者,让马跳起来

  •  0
  • Kristoffer Bohmann  · 技术社区  · 17 年前

    ::

    class A {
      function horse() {
        echo "horse";
      }
    }
    
    class B {
      function jump() {
        // $A = new A; ... don't want to add this in each method.
        $A->horse();  // Fails - $A is out of scope ($A = new A;).
        // A::horse();  // Old code style - works.
        // $this->horse();  // Works if you extend A - not self-documenting.
        // $this->A->horse();  // Fails - out of scope.
      }
    }
    
    $A = new A;
    $B = new B; // Better to use "$B = new B($A);" ?
    $B->jump(); // fails - the horse is sleeping.
    

    编辑

    一些现实世界的例子:

    • 跨类传递的数据库对象。
    • class url {
          function anchor($url,$name) {
              return "<a href="{$url}\">{$name}</a>";
          }
      }
      
      class someclass {
          function text($str,$url) {
              return "{$str}. " . $url->anchor($url,"Read more...");
          }
      }
      
    4 回复  |  直到 14 年前
        1
  •  1
  •   André Hoffmann    17 年前

    <?php
    class C extends A,B {
    
    //...
    
    }
    

    不

    strategy pattern

    更新 :

    singleton pattern

    class A
    {
    
        protected static $_instance;
    
        protected function __construct() //prohibit creating instances from outside
        { }
    
        public static function getInstance() 
        {
          if( self::$_instance === NULL ) {
            self::$_instance = new self();
          }
          return self::$_instance;
        }
    }
    
    $instance = A::getInstance();
    

    A::getInstance()

    :

    registry 如果可以有多个db连接。如果你绝对确定你总是只需要一个db连接,那么你也可以将其设置为单例。

        2
  •  0
  •   Daryl    17 年前

    我认为这应该奏效:

    $B = new B();
    $B->jump();
    

    http://www.php.net/manual/en/language.oop5.php

    当然,如果您从其他php文件访问该类,则应该导入该类。如果你在对象中,你调用的方法应该使用

    $this->jump();
    
        3
  •  0
  •   Benjamin Ortuzar    17 年前
        4
  •  0
  •   VolkerK    17 年前

    aggregation in COM :

    "magic method" __call

    class Foo {
      protected $inner = null;
      public function __construct($inner=null) {
        if ( is_null($inner) && !is_object($inner) ) {
          throw new Exception('...');
        }
        $this->inner = $inner;
      }
    
      public function __call($name, $arguments) {
        // also check http://uk.php.net/is_callable
        if ( !is_null($this->inner) && method_exists($this->inner, $name) ) {
          return call_user_func_array( array($this->inner, $name), $arguments);
        }
        else {
          // add some error handler here
          throw new Exception('...');
        }
      }
    
      function jump() {
        $this->horse();
        echo " jumps";
      }
    }
    
    class Bar {
      function horse() {
        echo "horse";
      }
    }
    
    $foo = new Foo(new Bar);
    $foo->jump();
    

    这行得通。但我只会在非常具体的情况下推荐这样的东西。最明显的原因是很难从外部判断这个对象$foo到底能做什么,不能做什么。