Forum Moderators: coopster

Message Too Old, No Replies

Classes and Constructors in PHP

Quick question on calling constructors

         

Nick_W

11:41 am on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi

I don't seem to be able to get onto the manual this morning so here's a quickie for any that know....

if I create a constructor for a class and call it like this:

$new = new myclass();

The constructor function is executed right?

But, if I call it like this:

$new = new myclass;

Will I get an instance of the object without executing the constructor?

Many thanks

Nick

Nick_W

1:14 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, okay, I'm lazy :(

Just tested it and for any that were wondering, they are exactly the same, the construtor is called regardless of how you invoke a new instance of the object...

Nick

jatar_k

5:26 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yep, when you create a new instance of an object it's constructor is called by default.

Otherwise it can't be constructed. ;)

Nick_W

5:30 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



heh, yes.

I was just looking for a neat way to invoke an instance for two differnent situations. One where the constructor was needed (although it could just be a regular method) and one where it was not.

No bit deal to just make a method and call when needed, but, it seemed logical at the time....

Nick

andreasfriedrich

10:36 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As the constructor is just a method that happens to get called when a new instance of a class is created and is not even required it is indeed a good idea to use just some other method when you need a piece of code only for some instances and not for every single one.

Andreas

jatar_k

10:38 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



a class can have multiple constructors to handle different instances, can't it? (been a little bit since I did OO).

I seem to remember different constructors within a class based on how the new object was created, be it type or method.

Nick_W

10:42 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dont think so Jatar

Though I've done constructors that took values and acted diffently according to those values which is kinda the same..

funciton constructor($val) {
if($vall==1) {
do one thing
} else do another

You get the point ;)

Nick

jatar_k

10:50 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe not php but I'm looking around on G and it seems to be true.

I remember C++ being like that. Depending on the number of params passed and what they are the object using a different constructor. Though I could also be losing my mind. ;)

andreasfriedrich

11:05 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In Java you could indeed do something like this:

public class Aaron { 
public Aaron(String s, int a) {
System.out.println('two parameter');
}
public Aaron(String s) {
System.out.println('one parameter');
}
}
Aaron a = new Aaron('Carter', 15);
would print two parameter while
Aaron a = new Aaron('Carter');
would print one parameter.

However, this is not possible in PHP.

class Aaron { 
function Aaron($str1, $str2) {
echo "two paras:";
}
function Aaron($str1) {
echo "one para:";
}
}
This will always print "one para" no matter whether you create the instance using two parameters or just one. The second declaration of Aaron will override the first one.

Andreas

Nick_W

11:08 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can overload a constructor? (is that the right term?)

Nick

andreasfriedrich

11:11 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, thatīs exactly the right term: A method having the same name and being distinguished by its signature, i.e. its different types and/or numbers of arguments or arguments in different position in its argument list.

This is available in C++ and Java. At least those are the ones I know of.