php 4.4.1 constructor Vs. Php 5.0 constructor

php 4.4.1 constructor Vs. Php 5.0 constructor

am 07.12.2005 02:35:17 von 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.

Re: php 4.4.1 constructor Vs. Php 5.0 constructor

am 07.12.2005 02:56:01 von 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

Re: php 4.4.1 constructor Vs. Php 5.0 constructor

am 07.12.2005 10:57:51 von 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

Re: php 4.4.1 constructor Vs. Php 5.0 constructor

am 07.12.2005 11:25:08 von 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

Re: php 4.4.1 constructor Vs. Php 5.0 constructor

am 08.12.2005 02:32:22 von 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

Re: php 4.4.1 constructor Vs. Php 5.0 constructor

am 08.12.2005 10:38:41 von 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:: with parent::]."

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

Re: php 4.4.1 constructor Vs. Php 5.0 constructor

am 09.12.2005 16:41:41 von 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

Re: php 4.4.1 constructor Vs. Php 5.0 constructor

am 09.12.2005 16:43:46 von Andy

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