Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PHP] Initializing class properties

0 views
Skip to first unread message

Lance Lovette

unread,
Nov 29, 2000, 2:18:22 PM11/29/00
to
Before PHP 4 I could initialize class properties that were objects like
this:

class A
{
};

class B
{
var $x = new A;
};

PHP 4 gives me a parse error when trying to initialize $x. Is there a way to
make PHP 4 work like it used to or must I move all object property
initializations to the class constructor?

Lance


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general...@lists.php.net
For additional commands, e-mail: php-gene...@lists.php.net
To contact the list administrators, e-mail: php-lis...@lists.php.net

Daniel Convissor

unread,
Nov 29, 2000, 9:27:22 PM11/29/00
to
Hi Lance:

> class B
> {
> var $x = new A;
> };
>
> PHP 4 gives me a parse error when trying to initialize $x. Is there a way to
> make PHP 4 work like it used to or must I move all object property
> initializations to the class constructor?

Yep, you'll have to do it in a constructor. Var's can only be set to strings
or numbers. Setting it using a variable, function, math, etc are not legal.

When I ran into this after converting from 3 to 4 it took me a LOOOOOONG time
of hacking to track it down.

Enjoy,

--Dan

--
PHP scripts that make your job easier
http://www.analysisandsolutions.com/code/
SQL Solution | Layout Solution | Form Solution
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Ave, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409

0 new messages