[PHP] unexpected T_NEW on object property

2006-04-21 Thread Paul Barry
With php5, I'm trying to create an object that has a property that is
another object.  First I have this class:

?php
class Address {

public $address1;
public $address2;
public $city;
public $state;
public $zip;

}
?

Then I have another class:

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

Then if I try to use the user object like this:

?php
require_once('model/User.class.php');

$user = new User();
$user-name = 'Paul Barry';
$user-address-city = 'Washington';

?
?= $user-name ? lives in ?= $user-address-city ?

I get this error:

Parse error: syntax error, unexpected T_NEW in /app/model/User.class.php on
line 5

What am I doing wrong?


Re: [PHP] unexpected T_NEW on object property

2006-04-21 Thread Richard Lynch
On Fri, April 21, 2006 2:17 pm, Paul Barry wrote:
 public $address = new Address();

I believe this is true:

At this time, you can only initialize class properties to CONSTANTS.

So you could use 'Address' or 42 or NULL or TRUE/FALSE, but not new
Address();

Actually, I think an array might also be do-able, come to think of it...

But for sure, the error message is saying you can't have 'new' there.

You'll have to initialize $this-address in the constructor.

-- 
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] unexpected T_NEW on object property

2006-04-21 Thread Jochem Maas

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



Re: [PHP] unexpected T_NEW on object property

2006-04-21 Thread M. Sokolewicz

Jochem Maas wrote:

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.
just to nag, an array is not a scalar value. However, you're correct on 
this. Properties can only be defined in the class with constant values 
(this does not mean they have to be constants! The values they get just 
have to be fixed, and not determined during runtime.)




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



Re: [PHP] unexpected T_NEW on object property

2006-04-21 Thread Jochem Maas

M. Sokolewicz wrote:

Jochem Maas wrote:


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.


just to nag, an array is not a scalar value. However, you're correct on 
this. Properties can only be defined in the class with constant values 
(this does not mean they have to be constants! The values they get just 
have to be fixed, and not determined during runtime.)


IC - spot the self taught idiot :-) (that's me btw)

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