1

どう聞いたらいいのかもわかりません。私はこれを常に C# で行っていますが、私の人生では、PHP で適切な例を見つけることができません。手伝ってくれますか?

私がやろうとしていることは、例えばです。多くのEmployeesを持つ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...

誰かがガイダンスを提供してもらえますか?

ありがとう

4

3 に答える 3

3

バージョン 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";
}
于 2010-08-02T00:13:18.350 に答える
2
foreach ($company->getEmployees() as $emp) {
    //do something with $emp
}

を参照してくださいforeach

1 つのメモ:

    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;
    }

フィールドfirstおよびlastは、初期化子を持たないため、 に初期化されるためnullです。isset、あなたの場合、引数がnullかどうかのみをチェックします。これは、設定されることが保証されているためです(引数です)。

于 2010-08-02T00:10:51.240 に答える
2

PHP には、PHP 標準ライブラリと呼ばれるものが言語に組み込まれています。PHP 標準ライブラリには、特定のコア言語機能を独自の定義済みオブジェクトに実装できるようにする多数のインターフェイスが付属しています。これらの 1 つはiteratorインターフェイスです。このインターフェイスを実装するクラスを定義してから、オブジェクトが foreach ループに配置されたときに必要なことを実行できるようにする実装を記述します。

また、あなたが求めているセマンティックに応じて、同じように簡単に行うことができます

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

getEmployeesメソッドは 1 回だけ呼び出されます。

于 2010-08-02T00:14:47.277 に答える