2010-12-20 53 views
0

我有类“User_registration”,在这个类中我需要使用许多类:“位置”,“链接”,“邮件”,“模块”。在一个类中运行很多类,PHP OOP

我创建包含文件中的所有类: include'class/location.php'; 包含'class/links.php'; 包含'class/mail.php'; 包含'class/modules.php';

现在创建“User_registration”类。

<?php 
class User_registration{ 

    public function insert_to_db($name, $country_code, $home_page) 
    { 
     //work with data 

     return id; 
    } 

    public function show_info($id) 
    { 
     //work with data 
    } 

} 

$reg_u = new User_registration; 
$res = $reg_u->insert_to_db($name, $country_code, $home_page); 

if($res){ 
    $reg_u->show_info($res); 
} 
?> 

我需要在方法“insert_to_db”跑班:“位置”,“链接”,“邮件”的方法 和“show_info”运行“位置”的一些方法,“链接”,“模块”类。

怎么样?如何在一个类中运行的另一个类(no't一个)

感谢您的帮助;)

+0

您可以创建对象的一个​​实例并调用它们的方法,或者如果它适合使这些方法成为静态并静态调用它们。 – 2010-12-20 21:48:44

回答

2

有几个方法可以做到这一点。如果您只有其他类需要使用的几个对象,请使用依赖注入;将每个对象作为参数传递给类的构造函数,并将这些对象存储为类属性。

如果只有一个方法需要该对象,则将该对象作为方法的参数传递。尽管我不鼓励这种方法,因为从长远来看,我觉得它妨碍了可扩展性/代码清洁。

如果你有很多对象需要在几个类中,我推荐你注入一个类的构造函数的注册表。注册表是一个单例(它拥有您需要共享的每个对象的单个实例)。在需要使用共享对象的类中,您可以调用$this->registry->get('Some_Shared_Object')->doSomething()


依赖注入使用

class Foo { 
    protected $dependency1; 
    protected $dependency2; 
    protected $dependency3; 

    public function __construct($dependency1, $dependency2, $dependency3) { 
     $this->dependency1 = $dependency1; 
     $this->dependency2 = $dependency2; 
     $this->dependency3 = $dependency3; 
    } 

    public function foo() { 
     $this->dependency1->doSomething(); 
    } 
} 

$foo = new Foo($dependency1, $dependency2, $dependency3); 
$foo->foo();

依赖注入(在该方法中,不推荐)

class Foo { 
    public function foo($dependency1) { 
     $dependency1->doSomething(); 
    } 
} 

$foo = new Foo(); 
$foo->foo($dependency1);

依赖注入注册表

(在构造函数)
class Registry { 
    var $data = array(); 

    function __get($key) { 
     return $this->get($key); 
    } 

    function __set($key, $value) { 
     $this->set($key, $value); 
    } 

    /** 
    * Retrieve a resource from the registry. 
    * 
    * @param string 
    * @return mixed|null 
    */ 
    function get($key) { 
     return isset($this->data[$key]) ? $this->data[$key] : NULL; 
    } 

    /** 
    * Store a resource in the registry. 
    * 
    * @param string 
    * @param mixed 
    */ 
    function set($key, &$value) { 
     $this->data[$key] = $value; 
    } 

    /** 
    * Check if a resource exists in the registry. 
    * 
    * @param string 
    * @return boolean 
    */ 
    function has($key) { 
     return isset($this->data[$key]); 
    } 
} 

class Foo { 
    protected $registry; 

    public function __construct($registry) { 
     $this->registry = $registry; 
    } 

    public function foo() { 
     $this->registry->dependency1->doSomething(); 
    } 
} 

$dependency1 = new Dependency1(); 
$registry = new Registry(); 
$registry->set('dependency1', $dependency1); 

$foo = new Foo($registry); 
$foo->foo();
+0

谢谢,但也许有一些例子? – lolalola 2010-12-21 06:34:29

+0

使用示例编辑 – simshaun 2010-12-21 15:49:30

1

作为一种良好的做法,我从类中调用一个类时总是使用include_once/require_once。这样,我知道无论在哪里使用它的参考资料都会被照顾,并且不会在一圈之内。

那么初始化每个实例并从那里调用你的方法。不要害怕静态引用。

+0

您是否在讨论在类中包含文件(依赖项,例如类)?如果是这样,这对灵活性不利。 – simshaun 2010-12-20 21:56:21