【问题标题】:class object in class with php oop使用 php oop 类中的类对象
【发布时间】:2010-08-02 00:06:59
【问题描述】:

我什至不知道该怎么问。我一直在 C# 中这样做,但在我的一生中,我在 PHP 中找不到任何像样的示例。你能帮忙吗?

例如,我正在尝试做的是。假设我有一个名为 Company 的类,它有很多员工。

我希望能够将所有员工存储在 company 类中,然后当我遍历 Company 对象时,我需要能够遍历分配给该公司的所有员工,并且我有很多一段时间试图找到一个直接的例子。

我可以填充它,但从我的生活中我无法遍历员工。我知道我做错了什么,但无法弄清楚。这是我的例子。

员工

据我所知,这可能是完全错误的,因为要恢复数据。

    class Employee
    {
        private $first;
        private $last;

        public function __construct($first = null, $last = null)
        {
            if(isset ($first))
                $this->first = $first;

            if(isset ($last))
                $this->last = $last;
        }

        public function getEmployee()
        {
            return Employee($this->first, $this->last);
        }

        public function getFirst()
        {
            return $this->first;
        }

        public function getLast()
        {
            return $this->first;
        }
    }

公司有很多员工。

在 c# 中我会使用类似泛型的东西

    public List<Employee> Employees {get;set;}

然后在构造函数中我会做类似的事情

    Employees = new List<Employee>();

然后我可以做类似的事情

    foreach(var x in Customer.Employees)
            x.first;

这就是我想要做的。

    class Company
    {
        private $companyName;

        private $employees;


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

        public function setEmployee(Employee $employee)
        {
            $this->employees[] = $employee;
        }

        public function getCompanyName()
        {
            return $this->companyName;
        }

        public function setCompanyName($nameOfCompany)
        {
            $this->companyName = $nameOfCompany;
        }

        public function getEmployees()
        {
            return $this->employees;
        }
    }

创建和填充

    $c = new Company("Test");
    $c->setEmployee(new Employee('A','B'));
    $c->setEmployee(new Employee('C','D'));
    $c->setEmployee(new Employee('E','F'));

到目前为止一切都很好。

获取公司名称没问题。

    echo $c->getCompanyName();

    echo "<PRE>";
    print_r($c);
    echo "</PRE>";

但我如何循环访问员工?

我希望能够做一些事情,比如将公司员工循环投射到单个员工,然后做类似的事情

    foreach(customers employees)
           echo Employee->getFirst, etc...

有人可以提供一些指导吗?

谢谢

【问题讨论】:

    标签: php oop programming-languages class arraycollection


    【解决方案1】:

    版本 1:显式 getter 方法

    <?php
    class Employee
    {
      private $first;
      private $last;
    
      public function __construct($first = null, $last = null)
      {
        if(isset ($first))
        $this->first = $first;
    
        if(isset ($last))
        $this->last = $last;
      }
    
      public function getFirst()
      {
        return $this->first;
      }
    
      public function getLast()
      {
        return $this->first;
      }
    }
    
    class Company
    {
      private $companyName;
      private $employees;
    
      public function __construct($nameOfCompany)
      {
        $this->companyName = $nameOfCompany;
      }
    
      public function addEmployee(Employee $employee)
      {
        $this->employees[] = $employee;
      }
    
      public function getCompanyName()
      {
        return $this->companyName;
      }
    
      public function getEmployees()
      {
        return $this->employees;
      }
    }
    
    $company = new Company('ACME');
    $company->addEmployee(new Employee('A', 'A'));
    $company->addEmployee(new Employee('B', 'B'));
    $company->addEmployee(new Employee('C', 'C'));
    
    foreach( $company->getEmployees() as $e ) {
      echo $e->getFirst(), "\n";
    }
    

    版本 2:使用 __get

    class Company
    {
      private $companyName;
      private $employees;
    
      public function __construct($nameOfCompany)
      {
        $this->companyName = $nameOfCompany;
      }
    
      public function addEmployee(Employee $employee)
      {
        $this->employees[] = $employee;
      }
    
      public function __get($name) {
         // this will allow access to any member of the class
         // so you might want to test access first
         if ( isset($this->$name) ) {
           return $this->$name;
         }
       }
    
    }
    
    $company = new Company('ACME');
    $company->addEmployee(new Employee('A', 'A'));
    $company->addEmployee(new Employee('B', 'B'));
    $company->addEmployee(new Employee('C', 'C'));
    
    foreach( $company->employees as $e ) {
      echo $e->getFirst(), "\n";
    }
    
    $company = new Company('ACME');
    $company->addEmployee(new Employee('A', 'A'));
    $company->addEmployee(new Employee('B', 'B'));
    $company->addEmployee(new Employee('C', 'C'));
    
    foreach( $company->employees as $e ) {
      echo $e->getFirst(), "\n";
    }
    

    【讨论】:

    • 不要用__get 给他想法——至少不要告诉他内在的问题。
    • 非常感谢您的回复。它让我朝着正确的方向前进。问题,是因为 __get 允许你这样做吗,foreach($company->employees as $e)?我想说是的,但我只是想确保我理解它。还有什么陷阱? Artefacto提到了内在的问题?再次感谢您
    • @nite __get 比直接访问对象属性要慢得多(在下一版本的 PHP 中会更慢)。在繁重的面向对象应用程序中,它可能会有所作为。
    • @nitefrog:是的,这是因为 __get($name) 方法。当某些东西试图访问对象的不可访问属性时,会调用此方法。因此,至少请记住,如果您有类似 class Foo { public $bar; } .... echo $foo-&gt;bar; 的内容,则不会调用该方法
    【解决方案2】:
    foreach ($company->getEmployees() as $emp) {
        //do something with $emp
    }
    

    foreach

    一个注释:

        public function __construct($first = null, $last = null)
        {
            if(isset ($first))
                $this->first = $first;
    
            if(isset ($last))
                $this->last = $last;
        }
    

    完全一样
        public function __construct($first = null, $last = null)
        {
            $this->first = $first;
            $this->last = $last;
        }
    

    因为没有初始化器的字段firstlast 被初始化为nullisset,在你的情况下,只检查参数是否为空,因为它保证被设置(它是一个参数)。

    【讨论】:

    • 谢谢你的回答。我想我正在尝试做一个强大的 $emp 类型。例如像 (Employee)$emp,或者这在 PHP 中真的不可能吗?再次感谢您的快速回复。
    • @nite 如果您要问的话,这些转换在 PHP 中是不可能的。
    【解决方案3】:

    PHP 在语言中内置了称为 PHP 标准库的东西。 PHP 标准库带有许多接口,允许您将某些核心语言功能实现到您自己定义的对象中。其中一个是iterator 接口。定义一个实现这个接口的类,然后编写一个实现,当你的对象被放置在一个 foreach 循环中时,它可以做你想做的事情。

    另外,根据你所追求的语义,你可以很容易地做一个

    foreach($company->getEmployees() as $employee)
    {
        var_dump($employee);
    }
    

    getEmployees 方法只会被调用一次。

    【讨论】:

    • 你好艾伦,谢谢你的休息。我不知道迭代器。自从我做任何 PHP 和来自其他语言的人以来已经有很长时间了,这有点令人困惑。再次感谢...
    猜你喜欢
    • 2012-07-15
    • 2014-10-08
    • 1970-01-01
    • 2013-08-03
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2017-05-11
    相关资源
    最近更新 更多