Class object in class with php oop?

Version 1: explicit getter method 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"; } version 2: using __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"; }.

Don't give him ideas with __get -- at least not without telling him about the inherent problems. – Artefacto Aug 2 '10 at 0:20 Thank you very much for this response. It gets me in the right direction.

Question, is it because of the __get that allows you to do this, foreach( $company->employees as $e )? I want to say yes, but I just want to make sure I understand it. Also what are the gotchas?

Artefacto mentioned inherent problems? Thank you again – nitefrog Aug 2 '10 at 2:54 @nite __get is much slower than direct access to an object property (and will be even more so in the next version of PHP). In a heavy object-oriented application, it will probably make a difference.

– Artefacto Aug 2 '10 at 7:03 @nitefrog: Yes, it's because of the __get($name) method. This method is called when something tries to access an inaccessible property of the object. So, keep at least in mind that the method will not be called if you have something like class Foo { public $bar; } .... echo $foo->bar; – VolkerK Aug 2 '10 at 7:51.

Foreach ($company->getEmployees() as $emp) { //do something with $emp } See foreach. One note: public function __construct($first = null, $last = null) { if(isset ($first)) $this->first = $first; if(isset ($last)) $this->last = $last; } is exactly the same as public function __construct($first = null, $last = null) { $this->first = $first; $this->last = $last; } because the fields first and last, having no initializer, are initialized to null. Isset, in your case, only checks if the argument's null, since it's guaranteed to be set (it's an argument).

Thank you the answer. I guess I was trying to do a strong type of $emp. For example like (Employee)$emp, or is that not really possible in PHP?

Again thank you so much for the quick response. – nitefrog Aug 2 '10 at 2:46 @nite Those casts are not possible in PHP if that's what you're asking. – Artefacto Aug 2 '10 at 3:16 ok, now it makes sense... – nitefrog Aug 2 '10 at 3:34.

PHP has something called the PHP Standard Library built into the language. The PHP Standard Library comes with a number of interfaces that allow you to implement certain core language functionality into your own defined objects. Once of these is the iterator interface.

Define a class that implements this interface, and then write an implementation that will allow your object to do what you want when it's placed in a foreach loop. Also, depending on the semantic you're after, you could just as easily do a foreach($company->getEmployees() as $employee) { var_dump($employee); } The getEmployees method will only be called once.

Thank you for the repose. I was not aware of the iterator. It has been a long time since I have done any PHP and man coming from other languages to this is a little confusing.

Thank you again... – nitefrog Aug 2 '10 at 2:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions