onlyican Posted November 14, 2006 Share Posted November 14, 2006 Hey guysThis is a bit strangeI been working on a large script, on my local machineI uploaded it to my server, and was getting erorrs left right and centreI found out that the __construct function is not running when I call the functionI thought I will double check this, so I built a dummy class.The code is something like thisFILE: myclass.php[code]<?phpclass test{ function __construct(){ echo "This is the first function, which should run automaticly<br />\n"; } function AddedFunction(){ echo "This function should only be called when I call it<br />\n"; }}?>[/code]thenFILE: run.php[code]<html><head><title>Test Class</title></head><body><?phprequire("myclass.php");echo "Here, I will call start the new object for the class<br /><br />\n\n";$dummy = new test;echo "Now I will call the Extra Function<br /><br />\n\n";$dummy->AddedFunction();echo "End<br />\n";?></body></html>[/code]Nothing too clever thereOn my local machine, which is running PHP V 5.1.1I get the following[quote]Here, I will call start the new object for the classThis is the first function, which should run automaticlyNow I will call the Extra FunctionThis function should only be called when I call itEnd[/quote]Which is correctBUTOn my server, which is running PHP V 4.4.3 I get[quote]Here, I will call start the new object for the classNow I will call the Extra FunctionThis function should only be called when I call itEnd[/quote]Which is missing the line from the construct functionAny ideas Link to comment https://forums.phpfreaks.com/topic/27200-__construct-not-working/ Share on other sites More sharing options...
onlyican Posted November 14, 2006 Author Share Posted November 14, 2006 Sorted nowIn php V4 the function __construct does not workWhat you can do, which works in V5 as wellName the function you want to construct the same as the class namefor example[code]<?phpclass MyClass{function MyClass(){}}[/code] Link to comment https://forums.phpfreaks.com/topic/27200-__construct-not-working/#findComment-124392 Share on other sites More sharing options...
Jenk Posted November 14, 2006 Share Posted November 14, 2006 __construct is only a magic function as of php5, the php4 syntax is to use the name of the class as the constructor:[code]<?phpclass FooBar{ /* constructor */ function FooBar() {}}?>[/code] Link to comment https://forums.phpfreaks.com/topic/27200-__construct-not-working/#findComment-124394 Share on other sites More sharing options...
obsidian Posted November 14, 2006 Share Posted November 14, 2006 Yes, there are actually a bunch of "Magic Methods" added to classes in PHP5. See [url=http://us2.php.net/manual/en/language.oop5.magic.php]this manual page[/url] for a full listing and what they do. Link to comment https://forums.phpfreaks.com/topic/27200-__construct-not-working/#findComment-124398 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.