prince198 Posted June 21, 2009 Share Posted June 21, 2009 hi to all i have litte problme with extend in php5 i want to create many class in many files in php 5 so for example i have 3 files class animal.php class animal { protected $age; protected $name; public function __construct($name, $age) { $this->age = $age; $this->name = $name; } public function displayAge() { echo $this->age; } public function dispalyname() { echo $this->name; ?> file dog.php class dog extends animal { public function scream() { echo 'ohhhh !'; } } and file index.php require_once 'fdog.php'; $dog = new dog('dog', 10); $dog->displayname(); $dog->displayAge(); $dog->scream(); but i have this errors Fatal error: Class 'animal ' not found i tried auto load but same error my question is how i call and extend class that they exist in many files . thanks for help Quote Link to comment https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/ Share on other sites More sharing options...
KevinM1 Posted June 21, 2009 Share Posted June 21, 2009 How did you write your autoload function? Quote Link to comment https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/#findComment-860923 Share on other sites More sharing options...
prince198 Posted June 21, 2009 Author Share Posted June 21, 2009 hi thanks for answer <?php function generic_autoload($class) { require_once 'animal.php'; } ?> i tried to write this function on dog class and also in file functions.php but both with no result same error Quote Link to comment https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/#findComment-860929 Share on other sites More sharing options...
KevinM1 Posted June 22, 2009 Share Posted June 22, 2009 hi thanks for answer <?php function generic_autoload($class) { require_once 'animal.php'; } ?> i tried to write this function on dog class and also in file functions.php but both with no result same error Ah, I see your problem. Autoload is the name of a 'magic' function. In order for PHP to use it correctly, you need to name it correctly: function __autoload($className) { } Notice the two underscore characters before the word autoload. Now, in order for it to load the correct class, you need to flesh it out: function __autoload($className) { require_once("$className"); } This assumes that all code (the class files and your index.php are in the same directory). Now that that's written, put the function in a file (something like config.php) and include it in whatever script you want (again, in this case index.php) require_once("config.php"); $dog = new dog('dog', 10); $dog->displayname(); $dog->displayAge(); $dog->scream(); So long as everything's in the same directory, it should work. Now, most people tend to put their class code in different directories to keep them portable. There's an easy way to do this. I tend to name my classes like "MP_Base_Exception." The underscores represent directory separators, so the Exception class resides in MP/Base/Exception.php. In order for autoload to get files like that, my function looks like: function __autoload($className) { $path = str_replace('_', DIRECTORY_SEPARATOR, $className); require_once("$path.php"); } So, no matter what class I call, so long as I keep with the convention that underscores = directory separators, I can always obtain the class I'm looking for (provided it exists). Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/#findComment-860936 Share on other sites More sharing options...
prince198 Posted June 22, 2009 Author Share Posted June 22, 2009 thanks for help .u are great i tried include_once and is work but i like the autoload that u written. last question for : function __autoload($className) { require_once("$className"); } what i have tu write in place $className? if i want of course to call ti all my classes cause it's dosen't work i written function __autoload($className) { require_once("animal.php"); } now it's work but what if i have 5 classes ?should i write 5 autoload? all my classes are in same path Quote Link to comment https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/#findComment-860940 Share on other sites More sharing options...
KevinM1 Posted June 22, 2009 Share Posted June 22, 2009 thanks for help .u are great i tried include_once and is work but i like the autoload that u written. last question for : function __autoload($className) { require_once("$className"); } what i have tu write in place $className? if i want of course to call ti all my classes cause it's dosen't work i written function __autoload($className) { require_once("animal.php"); } now it's work but what if i have 5 classes ?should i write 5 autoload? all my classes are in same path Nope. It seems like you're confused with how autoload works. Like I said earlier, autoload is a 'magic' function. It's invoked every time you attempt to create an object of a class that hasn't yet been encountered by the script. The argument that's passed to it is the name of the class you want to create an object from. Let's look at an example: $myDog = new Dog(); In our script, no other Dog objects have been created. So, PHP looks at the autoload function to see how to handle it. Thankfully, we have written our own autoload function: function __autoload($className) { require_once("$className"); } How does this work? Well, let's go through it slowly.... function __autoload($className) $className is the name of the class we're trying to instantiate (create an object from). In our case, Dog. { require_once("$className"); } This is the important step. Autoload is a 'magic' function because it assumes some things, and provides some additional, unwritten functionality for us. It assumes that the class provided by $className is also the name of a file that contains that class info for us. So, it automatically adds '.php' to the end of it. That's why it works. So, what happens when this is written: $myDog = new Dog(); $myCat = new Cat(); $myTyrannasaurus = new Tyrannasaurus(); ?? Autoload will automatically include Dog.php, Cat.php, and Tyrannasaurus.php. You just need to make sure your class names and file names are the same. Quote Link to comment https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/#findComment-860947 Share on other sites More sharing options...
prince198 Posted June 22, 2009 Author Share Posted June 22, 2009 ah ok tnaks i realize that mu files and my classes have no the same name tnahk you a lot Quote Link to comment https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/#findComment-860948 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.