Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/163170-solved-problem-extends-in-php-5/
Share on other sites

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.

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

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.

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.