KevinM1 Posted December 5, 2006 Share Posted December 5, 2006 I'm trying to test the autoload function, but I keep getting an error. I have the following test code:[code]<?phpinclude("config.php");echo "Autoload test<br />\n";$item = new Product('name', '24.99', '3');foreach($item as $key => $value){ echo "$key -- $value<br />\n";}?>[/code]My config.php file is merely:[code]<?phpfunction __autoload($className){ include("/PHP_stuff/classes/{$className}.php");}?>[/code]But I keep getting the following error:[quote]Fatal error: Cannot instantiate non-existent class: product in /home/nights/www/www/PHP_stuff/test2.php on line 7[/quote]According to my web hosting, I have access to both PHP 4.4.4 and 5.1.6...could not using the right version (presumably 5.1.6) be the cause of my problem? If so, how can I switch versions? If not, what am I doing wrong?Thanks :)EDIT: My product class, in case that matters:[code]<?phppublic class Product{ private $Name; private $Price; private $Quantity; public function __construct($name, $price, $quantity){ this -> Name = $name; this -> Price = $price; this -> Quantity = $quantity; } public function getPrice(){ return (this -> Price) * (this -> Quantity); }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/ Share on other sites More sharing options...
KevinM1 Posted December 5, 2006 Author Share Posted December 5, 2006 I hate to bump this, but I'd really like to know how to fix my error...it's annoying me greatly. Quote Link to comment https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/#findComment-135578 Share on other sites More sharing options...
.josh Posted December 5, 2006 Share Posted December 5, 2006 a lot of hosts differentiate php4 and php5 by the mime type. For instance, making a script to be parsed by php4 might use the regular blah.php extension, but if you want php5 to parse it, try blah.php5 (php5) as the extension. That would be the first thing I try. edit: also, your include in your __autoload is commented out. no include == no class being defined.. Quote Link to comment https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/#findComment-135616 Share on other sites More sharing options...
KevinM1 Posted December 5, 2006 Author Share Posted December 5, 2006 I'm not sure why my autoload include appeared to be commented out, as it wasn't when I posted my code. I never commented it out at all, actually.In any event, it looks like I've almost got it working. Now I have the following error:[quote]Parse error: syntax error, unexpected T_PUBLIC in /home/nights/www/www/PHP_stuff/classes/Product.php5 on line 3[/quote]My Product class still remains the same:[code]<?phppublic class Product{ private $Name; private $Price; private $Quantity; public function __construct($name, $price, $quantity){ this -> Name = $name; this -> Price = $price; this -> Quantity = $quantity; } public function getPrice(){ return (this -> Price) * (this -> Quantity); }}?>[/code]When I remove the 'public' before my class declaration, I then get a T_OPERATOR problem with the '->'. I believe my syntax is basically correct, though, as the '$' isn't used for an object's properties when they're accessed, correct? Quote Link to comment https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/#findComment-135655 Share on other sites More sharing options...
trq Posted December 5, 2006 Share Posted December 5, 2006 [code]<?phpclass Product{ private $Name; private $Price; private $Quantity; public function __construct($name, $price, $quantity){ $this->Name = $name; $this->Price = $price; $this->Quantity = $quantity; } public function getPrice(){ return $this->Price * $this->Quantity; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/#findComment-135709 Share on other sites More sharing options...
KevinM1 Posted December 5, 2006 Author Share Posted December 5, 2006 [quote author=thorpe link=topic=117447.msg479313#msg479313 date=1165354334][code]<?phpclass Product{ private $Name; private $Price; private $Quantity; public function __construct($name, $price, $quantity){ $this->Name = $name; $this->Price = $price; $this->Quantity = $quantity; } public function getPrice(){ return $this->Price * $this->Quantity; }}?>[/code][/quote]This gives me the following error:[quote]Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/nights/www/www/PHP_stuff/classes/Product.php5 on line 9[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/#findComment-135751 Share on other sites More sharing options...
KevinM1 Posted December 6, 2006 Author Share Posted December 6, 2006 Nevermind, I found the problem. Quote Link to comment https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/#findComment-136097 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.