Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

wwwxxxx.jpeg, xxxxdup, WWWXXX..APC site:board.issociate.de, WWWXXXAPC, WWWXXX .CMD, Wwwwxxx reemine, WWWXXX.VCBA, WWWXXX.VCBA, TheboL.wwwxxxxx, WWWXXXAPC

Links

XODOX
Impressum

#1: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-07 02:35:17 by Greg Scharlemann

I have a class with a constructor function that looks like so on my php
5.0 box:

public function __construct(....)

does this syntax change when I go back to php 4.4.1? I've never used
4.4.1 before, been solely 5.0 until now. It seems the 4.4.1 box is
giving me an error on that call.

I've been looking on google for a while for a simple answer, but must
not be searching for the right thing.

Thanks in advance.

Report this message

#2: Re: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-07 02:56:01 by Etienne Marais

Greg Scharlemann wrote:

> I have a class with a constructor function that looks like so on my php
> 5.0 box:
>
> public function __construct(....)
>
> does this syntax change when I go back to php 4.4.1? I've never used
> 4.4.1 before, been solely 5.0 until now. It seems the 4.4.1 box is
> giving me an error on that call.
>
> I've been looking on google for a while for a simple answer, but must
> not be searching for the right thing.
>
> Thanks in advance.

PHP 4.x supports only constructors that
have the same name as the class,

e.g.

class abc {

// Constructor
function abc() {
}

}

The chances are pretty good that you will
have to upgrade to php 5

--
Etienne Marais
Cosmic Link
South Africa

Report this message

#3: Re: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-07 10:57:51 by Andy

It's true that PHP4 requires a constructor to have the same name as the
class, but that doesn't mean you have to upgrade to PHP5 just to use
the __construct method. Simply add a new method to you class with the
same name as the class and use that to call the __construct method.

Something like this should work:

class MyClass
{
function __construct($foo, $bar)
{
echo $foo, $bar;
}

function MyClass()
{
$args = func_get_args();
call_user_func_array(array(&$this, '__construct'), $args);
}
}

Andy

Report this message

#4: Re: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-07 11:25:08 by Etienne Marais

Andy wrote:

> It's true that PHP4 requires a constructor to have the same name as the
> class, but that doesn't mean you have to upgrade to PHP5 just to use
> the __construct method. Simply add a new method to you class with the
> same name as the class and use that to call the __construct method.

Nice. What else can a person do to
ensure 'upward' compatibility in
php 4 classes ?

--
Etienne Marais
Cosmic Link
South Africa

Report this message

#5: Re: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-08 02:32:22 by Greg Scharlemann

Thanks for the help. It looks like it's really just the public
statement I had in front of __construct that was screwing everything
up. In any case, I like the use of the class name over the user of
__construct. I think it looks cleaner. Is there any advantage to using
one over the other?

greg

Report this message

#6: Re: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-08 10:38:41 by Colin Fine

Greg Scharlemann wrote:
> Thanks for the help. It looks like it's really just the public
> statement I had in front of __construct that was screwing everything
> up. In any case, I like the use of the class name over the user of
> __construct. I think it looks cleaner. Is there any advantage to using
> one over the other?
>
> greg
>
Yes.
The reason for the change is explained in "Upgrading to PHP5" Adam
Trachtenberg (O'Reilly), pp. 33-4:
"When constructors are tied to class names, it's easy to break code
without realizing it. In PHP4, to call the parent constructor inside of
your class, you must hardcode the parent class name. ....

"However, moving this class under a different parent, or even renaming
the parent, forces you to edie the constructor, replacing the call to
[parent::<classname> with parent::<new-classname>]."

Incidentally, Andy's suggestion will work, but is unnecessary. P. 23 of
the same book says:
"To ease the transition from PHP 4, if PHP 5 canot find a method names
__construct() within your object hierarchy, it revers to the PHP 4
constructor naming scheme and searches accordingly."

As you say, 'public' is not recognised by PHP 4.

Colin

Report this message

#7: Re: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-09 16:41:41 by Andy

Good to know about PHP5 searching for a constructor of the same name as
the class if the __construct() method isn't available. Thanks for
that, Chris!

Andy

Report this message

#8: Re: php 4.4.1 constructor Vs. Php 5.0 constructor

Posted on 2005-12-09 16:43:46 by Andy

Erm.. I mean't Colin, not Chris. Sorry about that! :)

Report this message