简体   繁体   English

在类声明中使用外部对象-PHP

[英]Using an external object inside a class declaration - PHP

So let's say I have two PHP classes : User & Connection . 假设我有两个PHP类: UserConnection

What they do is obvious from their names. 从他们的名字可以明显看出他们的所作所为。

Now suppose I have this code: 现在假设我有以下代码:

User.php

<?php

class User {

    static function get_email( $user_id ) {
        $conn_object = new Connection();
        $connection  = $conn_object->connection;
        // do stuff with the connection object
    }

}

?>

Connection.php

<?php

class Connection {

    public $connection;

    public function __construct() {
        $this->connection = // connect to the database here
    }

}

?>

main.php

<?php

require "Connection.php";
require "User.php";

$email = User::get_email( 4 ); // this doesn't work

?>

When I do something like that in main.php , I get a bunch of errors. 当我在main.php执行类似操作时,出现了很多错误。 When I var_dump( $conn_object ); 当我var_dump( $conn_object ); in the User.php file, I always get a null , like the file doesn't even "see" the Connection object, or know it exists. User.php文件中,我总是得到一个null ,就像该文件甚至没有“看到” Connection对象,或者知道它存在一样。

However, when I use the Connection class in main.php directly, I don't get any errors and everything runs smoothly. 但是,当我直接在main.php使用Connection类时,我没有得到任何错误,并且一切运行顺利。 This means my Connection object doesn't have any syntax errors or otherwise. 这意味着我的Connection对象没有任何语法错误或其他。

Is this kind of thing not allowed to be done in PHP? 是否不允许在PHP中完成这种事情? Does the language not allow the usage of a class in a class? 语言是否不允许在一个类中使用某个类?

Remember classes share methods or properties by inheritance or depencency injection. 请记住,类通过继承或依赖注入来共享方法或属性。

<?php

class Connection {

    protected $connection;
    //we changed protected because only direct inherit can acces

    public function __construct() {
        $this->connection = // connect to the database here
   }

}

<?php

include connection.php...

class User extends Connection{

      function __construct(){
        parent::contruct();
      }

    static function get_email( $user_id ) {

        $this->connection->yourDbstuff ;
        // do stuff with the connection object
    }

}

?>

FYI: I try to answer a question as if I was asking it for the first time and try to include as much information as would be needed by someone who does not have as much knowledge as you or I. It makes it easier for those just learning to find answers. 仅供参考:我尝试回答的问题就好像是我第一次问这个问题一样,并尝试包含不如您或我那么多知识的人所需要的尽可能多的信息。这对于那些只是我的人来说更容易学习寻找答案。

require "Connection.php";

require "User.php";

$email = User::get_email( 4 ); // this doesn't work

The '::' references a parent method so if you have not extended the Connection class with the User class, it will not work. '::'引用了一个父方法,因此,如果您还没有使用User类扩展Connection类,它将无法正常工作。 And even if you have, it is not guaranteed to work, which more likely means that I did something wrong when trying it. 即使有,也不能保证它能正常工作,这很可能意味着我在尝试时做错了什么。

To use an external connection object, you would need to pass the connection object to the user object so that the user object can use the connection object as follows. 要使用外部连接对象,您需要将连接对象传递给用户对象,以便用户对象可以按如下方式使用连接对象。

$thisConnection = new Connection(); $thisUser = new User(); $thisUser = new User($thisConnection); or $thisUser->setConnection($thisConnection); $thisUser->setConnection($thisConnection);

Then within your user class you would need a single method or a group of methods that duplicates the methods within the connection class. 然后,在用户类中,您将需要一个方法或一组方法来复制连接类中的方法。

However, as was mentioned by Joaquin Javi, Object Oriented Programming uses inheritance and child objects inherit and have access to parent object methods. 但是,正如Joaquin Javi提到的那样,面向对象编程使用继承,子对象继承并可以访问父对象方法。

In addition, by extending the connection class with the user class, you save yourself a whole lot of coding. 另外,通过使用用户类扩展连接类,可以节省大量代码。

On the other hand, if you have a specific reason you do not want to use extends and the inheritance shortcut, you will need to pass the Connection object into the User object. 另一方面,如果您有特定的原因不想使用扩展和继承快捷方式,则需要将Connection对象传递给User对象。

Just to be clear, when you extend the Connection class with the user class, the user class will inherit the methods as well as the variables of the connection class as long as they are set to public or protected, and you will be able to access any variable or method such as the get_email method as follows: 只需说明一下,当使用用户类扩展Connection类时,用户类将继承方法以及连接类的变量,只要它们被设置为public或protected,并且您将能够访问任何变量或方法,例如get_email方法,如下所示:

$thisUser->get_email( 4 );

Hope this helps. 希望这可以帮助。 Scott 史考特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM