Re: unexpected T_NEW on object property

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Paul Barry wrote:

..


Then I have another class:

<?php
require_once('model/Address.class.php');
class User {
    public $name;
    public $address = new Address();

this is wrong. you can define the property in the class
with a constant or scalar value (i.e. literal string,
numeric value or an array) but not a return value of a
function or a 'new' object.

you should initialize the $address property in the contructor
of the User object like so:

class User {
     public $name;
     public $address;

     function __construct($name = '')
     {
	$this->name = strval($name);
        $this->address = new Address;
     }
}

it's good practice to only set values to the objects
properties once it's contructed (or while it's being
constructed - as per my example).

}
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux