473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

php 4.4.1 constructor Vs. Php 5.0 constructor

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.

Dec 7 '05 #1
7 2252
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

Dec 7 '05 #2
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

Dec 7 '05 #3
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

Dec 7 '05 #4
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

Dec 8 '05 #5
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
Dec 8 '05 #6
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

Dec 9 '05 #7
Erm.. I mean't Colin, not Chris. Sorry about that! :)

Dec 9 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.