Jump to content

Recommended Posts

I'm trying to test the autoload function, but I keep getting an error.  I have the following test code:
[code]
<?php

include("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]
<?php

function __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]
<?php

public 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]
Link to comment
https://forums.phpfreaks.com/topic/29544-autoload-problem-solved/
Share on other sites

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..
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]
<?php

public 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?
[code]
<?php

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 author=thorpe link=topic=117447.msg479313#msg479313 date=1165354334]
[code]
<?php

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]

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.