简体   繁体   English

PHP OOP- 对象交互

[英]PHP OOP- Object Interaction

I am currently working in PHP and have begun to utilise classes- I am learning a lot, however I am yet to have my "penny drop" moment- I am struggling with object interaction.我目前正在使用 PHP 并开始使用类 - 我学到了很多东西,但是我还没有遇到“一分钱一分货”的时刻 - 我正在努力处理对象交互。

I'd really appreciate some clarification in regards to how one object should interact with another.我真的很感激关于一个对象应该如何与另一个对象交互的一些澄清。 As a very basic example if I have a database object which handles connecting and querying a database, how should other classes access the results of queries performed by the database object?作为一个非常基本的例子,如果我有一个处理连接和查询数据库的数据库对象,其他类应该如何访问数据库对象执行的查询结果?

From my understanding so far, I can see 2 options.根据我目前的理解,我可以看到 2 个选项。 Either the query results within the database class are accessible publicly and are passed to other objects as arguments...数据库类中的查询结果可以公开访问并作为参数传递给其他对象......

$databaseObject = new DatabaseObject;
$databaseObject->query("query goes here");

$newObject = randomObject;
$newObject->doStuff($databaseObject->query())

Or objects which rely on query results instantiate new database objects within themselves and therefore the results are contained within that object.或者依赖于查询结果的对象在其自身内实例化新的数据库对象,因此结果包含在该对象中。

class databaseConnection {

    public $queryResults;

    function __construct {
        connect to database...
    }

    function query {
        perform queries...
        $this->queryResults = query results
    }
}

class NewObject
{
    function doStuff() {
        $db = new databaseConnection
        do stuff with $db->queryResults
    }
}

Option 1 seems wrong as properties in the database class are available globally, whereas option 2 seems to contradict the idea that classes should only hold methods and properties relating to the object they define.选项 1 似乎是错误的,因为数据库类中的属性是全局可用的,而选项 2 似乎与类应该只保存与它们定义的对象相关的方法和属性的想法相矛盾。

I could think of many other examples.我还能想到很多其他的例子。 If I create a class responsible for managing a user throughout a system, yet classes responsible for other tasks need to know what privilege level a user has, do I make that property within the user class global and pass it to other classes?如果我创建了一个负责在整个系统中管理用户的类,但负责其他任务的类需要知道用户具有什么权限级别,我是否在用户类中将该属性设为全局并将其传递给其他类? Do the other classes instantiate new user objects?其他类是否实例化新的用户对象?

Any help much appreciated.非常感谢任何帮助。

Either the query results within the database class are accessible publicly数据库类中的查询结果可以公开访问

This (for me) is the key to the answer.这(对我来说)是答案的关键。 If you set or change a value in the instance, this rather implies that you want to set the value in the underlying dataset (although you might consider defering the write).如果您在实例中设置或更改一个值,这更意味着您希望在基础数据集中设置该值(尽管您可能会考虑推迟写入)。 Note that the magic methods allow you to associate actions with get and set operations implicitly.请注意,魔术方法允许您隐式地将操作与 get 和 set 操作相关联。 But not every query is updateable.但并非每个查询都是可更新的。 If your sql is baked in, then you can determine the updatability at design time.如果您的 sql 已内置,那么您可以在设计时确定可更新性。

Also, I suggest that your query object has a set of record objects associated with it where you interact with the underlying data.此外,我建议您的查询对象具有一组与之关联的记录对象,您可以在其中与基础数据进行交互。 Bundling the connection functionality in the same object as the query only really makes sense if it is a factory (supporting multiple queries) and creating query result objects.将连接功能与查询捆绑在同一个对象中,只有当它是一个工厂(支持多个查询)并创建查询结果对象时才真正有意义。 And the query (result) object should probably implement the iterator interface.并且查询(结果)对象可能应该实现迭代器接口。

Both pdo and mysqli seperate the connection, query and results into seperate objects - for good reason. pdo 和 mysqli 都将连接、查询和结果分离到单独的对象中——这是有充分理由的。

class Connection {

  public static $conn;

  public static function getInstance() {
     if (!self::$conn) {
         self::$conn = new Connection();
     }

     return self::$conn;
  }

  public function query() {
      echo "run your query here using self::\$conn object";
  }
}

class Person {

    public function doStuff() {
        $conn = Connection::getInstance();
        $results = $conn->query();
    }
}

$person = new Person();
$person->doStuff();

Firstly, you want to get your database connection only one time rather then opening it every time.首先,您只想获得一次数据库连接,而不是每次都打开它。

Secondly, using the same database connection object you can run multiple queries as shown in the above example.其次,使用同一个数据库连接对象,您可以运行多个查询,如上例所示。

You will always get a single connection no matter how many other classes you use.无论您使用多少其他类,您将始终获得一个连接。

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

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