0

I would like to create a class with an instance of another class used as a property. Something like this:

class person
{
    var $name;
    var $address;
}

class business
{
    var $owner = new person();
    var $type;
}

This, of course, does not work, and I have tried a few variations. Everything I have found in Google searches only references nested class definitions (class a { class b{ } }).

Is it possible to put a class instance in another class? If not, is there a reasonable work-around?

miken32
  • 42,008
  • 16
  • 111
  • 154
Andrew
  • 467
  • 3
  • 7
  • 22
  • 2
    You instantiate your `person` class and set that as the `owner` property value in the `business` class constructor – Mark Baker Dec 17 '14 at 18:07
  • 2
    But stop using `var` and use proper [visibility](http://www.php.net/manual/en/language.oop5.visibility.php) keywords.... you're not still using PHP4 are you? – Mark Baker Dec 17 '14 at 18:08
  • I'm with Mark Baker here, explain why you can't use for instance business::__construct() to set the $owner property? Whether you create that instance of person in the construct or pass it into the constructor as a parameter? – qrazi Dec 17 '14 at 18:15
  • @qrazi - No particular reason. I am pretty good at procedural PHP and objective objective C#. This is my first attempt at objective programming in PHP. So I am figuring out what my limitations are. I will probably use a combination of everything mentioned as well as the get_class() function to disallow trying to set the $owner field to the wrong type. – Andrew Dec 17 '14 at 22:23
  • @Andrew i think you don't need get_class(). If you instantiate in the constructor, you know what class it is. If you take it as a parameter like suchit's answer, it would make sense to type hint that parameter. – qrazi Dec 17 '14 at 22:42

3 Answers3

3

You can't initialize classes as part of a attribute/property/field declaration.

From the PHP Property documentation:

[Property] declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

If you need the new instance to have a property that contains a class instance, you need to assign it in the constructor:

class business {
    public $owner;

    public function __construct() {
        $this->owner = new person();
    }
}

As an aside, be sure to use visibility keywords (public, protected, private) rather than var.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
2
class person
{
    public $name;
    public $address;
}

class business
{
    public $owner;
    public $type;

    public function __construct()
    {
        $this->owner = new person();
    }
}
mopo922
  • 6,293
  • 3
  • 28
  • 31
1

may be try using like this if your intention is to call other class method:

class person
{
    public $name;
    public $address;
}

class business {
 public $owner;
    function __construct( $obj ) {
        $this->owner = $obj;
    }

}   
$a = new person();
$b = new business($a);
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
  • This is probably the best answer. I will likely use something similar to this. My biggest concern was only allowing the person class in attribute $a. I will probably use the get_class() function to test the validity of the variable passed to the constructor. Thanks! – Andrew Dec 17 '14 at 22:29
  • 1
    @Andrew or use type hinting: `function __construct(person $obj)` see http://php.net/manual/en/language.oop5.typehinting.php – SirDarius Dec 18 '14 at 08:50