简体   繁体   English

PHP-OOP返回对象或数组?

[英]PHP - OOP return objects or array?

I'm just starting to learn OOP as my usual procedural approach is beginning to become messy when dealing with larger projects. 我刚开始学习OOP,因为在处理较大的项目时,我通常的程序方法开始变得混乱。 I've ran into a few things i'm unsure about early on. 我碰到了一些我不确定的事情。

The idea is simply adding new members to a database, and pulling a list of existing members from the database. 这个想法只是将新成员添加到数据库,然后从数据库中提取现有成员的列表。

I've put some comments in my code as well as the questions below. 我在代码中以及以下问题中添加了一些注释。

class memberManager
{
    public $members;

    public function createMember($db, $array)   // Should I pass in an array or seperate items?
    {
        // Db code


    }

    public function getAllMembers($db)
    {
        // Should I return an array, then loop through in my script creating the "member" objects, or should I create and return them directly in this function?
        $dbconnection = $db->prepare("SELECT * FROM members");
        $dbconnection->execute();

        $x = 0;
        while ($row = $dbconnection->fetch())
        {
           $members[$x]['firstname'] = $row['firstname'];
           $members[$x]['lastname'] = $row['lastname'];
           $members[$x]['dob'] = $row['dob'];
           $x++;
        }

        return $members;

    }
}


class member
{
    public $firstname, $lastname, $dob;

    function __construct($details)
    {
        $this->firstname = $details['firstname'];
        $this->lastname = $details['lastname'];
        $this->dob = $details['dob'];
    }
}

members-list.php 会员制list.php的

$memberManager = new memberManager();
$membersList = $memberManager->getAllMembers($db);

foreach ($membersList as $existingMember)
{
   $member = new member($existingMember);
}
  1. When I create a new member, should I pass in an array, or each item individually? 创建新成员时,应该传递数组还是单独传递每个项? My reasons for doing an array is it allows me to add new parameters to it easy enough, especially if I'm storing a lot of data about a member, as opposed to *function createMember($db, $firstname, $lastname, $dob, etc, etc.) 我做数组的原因是它使我可以很容易地添加新参数,特别是如果我存储有关成员的大量数据,而不是* function createMember($ db,$ firstname,$ lastname,$小球等)
  2. In my "getAllMembers" function, as it pulls each member should I put them into an array as I've done here, then loop through that array on my members-list.php creating objects, or should I instantiate the member() class directly within "getAllMembers", and just return an array of objects? 在我的“ getAllMembers”函数中,因为它拉动每个成员,所以我应该像在这里一样将它们放入数组中,然后在我的members-list.php中遍历该数组以创建对象,或者我应该实例化member()类直接在“ getAllMembers”中,仅返回对象数组?

Hopefully this makes sense, It's written in a bit of a pseudocode-ish manner just to highlight the problems. 希望这是有道理的,只是为了突出问题而以一种伪代码的方式编写。

  1. I will pass an array. 我将传递一个数组。 As you said, it is easier to add a new property. 如您所说,添加新属性更加容易。

  2. I will return an array of objects and I will have a member entity that it will make easier to work with the properties. 我将返回一个对象数组,并且我将拥有一个成员实体,这将使使用属性更加容易。 My answer is just a personal opinion. 我的回答只是个人意见。

Note: this question can have multiple different answers, since, it will depends on the developer preferences 注意:这个问题可以有多个不同的答案,因为这将取决于开发人员的偏好

I know I am not exactly answering your question, but for basic CRUD I would really recommend some ORM framework. 我知道我并没有完全回答您的问题,但是对于基本的CRUD,我真的会推荐一些ORM框架。

As for your question, I think using separate entity classes over arrays is way to go - you sacrifice flexibility but in return you can keep entity specific logic in one place, which goes well with DRY principle . 关于您的问题,我认为在数组上使用单独的实体类是一种方法-您牺牲了灵活性,但作为回报,您可以将实体特定的逻辑放在一个地方,这与DRY原理非常吻合。

Please also note that your class naming is slightly off. 另请注意,您的班级命名略有不同。 While PHP does not definitive any naming conventions, for class names PascalCase is mostly used. 尽管PHP并没有明确的任何命名约定,但对于类名, PascalCase却被广泛使用。

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

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