tags:

views:

307

answers:

6

My code:

class Address{
   public var $Street;
}

class Employee{
    public var $ID:
    public var $Name:
    public var $Address;
}

$myEmployee = new Employee();
$myEmployee->Address = new Address();

How do I access the the street now?

$street = $myEmployee->$Address->Street;
$street = $myEmployee->Address->Street;

Both give me warnings in Eclipse. I was thinking of:

$street = $myEmployee['Address']->Street;

Is this correct?

+3  A: 
$street = $myEmployee->Address->Street;

should work. What does Eclipse complain about, anyway?

Mike Hordecki
Sorry, no warning message, but it does not do auto-completion. Is this not supported in the PHP Plugin for Eclipse?
A: 

Why don't you create the Address in the constructor of the Employee class? I'm guessing you're going to be creating more than one Employee eventually and, instead of having two lines every time to create both an Employee and an Address, you can just create an Employee and revel in the comfort that the Address was already created and stored in $myEmployee->Address.

Stephen
+5  A: 
$myEmployee->Address->Street;

It is valid and Eclipse's warning system is quite strange regarding things like this. I usually turn them off in instances like this and let PHP do the error reporting for me.

Also eclipse does not support auto completion for what you need to do.

Ólafur Waage
Stupid question but doesn't eclipse auto-complete that? It should be able to correct?
patricksweeney
+5  A: 

As has been said, your suggestion to use:

$street = $myEmployee->Address->Street;

is perfectly valid. However, there are a few errors in your code which might have caused the problems. Firstly, public member variables are either declared like this:

public $id;

or this (deprecated):

var $id;

Your combination of the two is not valid PHP code.

Also, you have two colons (:) at line endings, which should be semicolons (;).

Hope that helps!

George Crawford
A: 

An alternative would be to use accessors and private data members:

class Address {
   private $Street;

   public function __construct() {}

   public function setStreet($Street) {
       $this->Street = $Street;
   }

   public function getStreet() {
      return $this->Street;
   }
}

/* ... same for Employee ... */

$myEmployee = new Employee();
$myEmployee->setAddress(new Address());

$street = $myEmployee->getAddress()->getStreet();

This would provide you with encapsulation in case you need to change your internal implementation later. If you expose your class members as public data members, you're locked into them once client code starts using your classes.

Refactoring would be easier this way, and it will also get rid of your warnings.

Rob Hruska
A: 

The reason for the lack of Eclipse code completion is that code completion requires type information.

In PHP this is accomplished either with type hints and commentated annotations.

I'm not sure what Eclipse plugin you are using, but if it's PDT check out the manual for code assist:

A simple comment annotation then is:

Above Employee you could add:

/**
 * @property Address $Address
 */
class Employee 
....
TK