Jump to content

how to instantiate one class in another class


hem

Recommended Posts

my problem is i have one class in a separate file. And i want to make an object of that class in another class which resides in a separate file. so the Question is how can i import the first class to the second class.

 

the first class name = CloneDemo1.php

and the second class name = TestCloneDemo1.php

I think you are trying to ask about "how do I include the files easily for multiple classes" for which I will respond:

 

http://nz.php.net/manual/en/language.oop5.autoload.php

 

Perhaps I am misunderstanding however with your use of file names and no class names.  People be more clear if this doesn't solve your problem.

"ya i want to know "how do I include the files easily for multiple classes"

i have one file like this--

 

<?php

 

class CloneDemo1

{

 

    private $employeeid;

    private $tiecolor;

 

 

    function setEmployeeId($empid)

    {

        $this->employeeid = $empid;

    }

 

    function getEmployeeId()

    {

        return $this->employeeid;

    }

 

    function setTieColor($tcolor)

    {

        $this->tiecolor = $tcolor;

    }

 

    function getTieColor()

    {

        return $this->tiecolor;

    }

}

 

and another one like this--

 

<?php

 

function __autoload($class)

    {

    require_once($CloneDemo1.class."php");

    }

 

 

class TestCloneDemo1

{

 

 

    $cloneObj1 = new CloneDemo1();

 

    $cloneObj1->setEmploeeId(15264);

    $cloneObj1->setTieColor("Black");

 

$cloneObj2 = clone $cloneObj1;

 

$cloneObj2->setTieColor("Navy Blue");

 

    printf("Employee id:%d<br />", $cloneObj1->getEmployeeId());

    printf("Employee tie color:%s<br />", $cloneObj1->getTieColor());

    printf("Employee tie color:%s<br />", $cloneObj2->getTieColor());

   

}

 

?>

 

Now i am getting some error like parse error T_FUNCTION etc... indicating the line whr i instantiate the object in the sencond class... Plz help me with exact code.... Thank u

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.