hem Posted June 24, 2009 Share Posted June 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/ Share on other sites More sharing options...
xtopolis Posted June 24, 2009 Share Posted June 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862435 Share on other sites More sharing options...
hem Posted June 24, 2009 Author Share Posted June 24, 2009 "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 Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862447 Share on other sites More sharing options...
Ken2k7 Posted June 24, 2009 Share Posted June 24, 2009 function __autoload($class) { require_once($CloneDemo1.class."php"); } What's $CloneDemo1.class? Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862449 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Share Posted June 24, 2009 sounds like something out of compiled java bytecode Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862451 Share on other sites More sharing options...
xtopolis Posted June 24, 2009 Share Posted June 24, 2009 You are not using the function correctly. The argument passed to the function is the name of the object trying to be instantiated. Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862458 Share on other sites More sharing options...
hem Posted June 24, 2009 Author Share Posted June 24, 2009 ok will anybody tell me whts the correct thing to do then.....how to solve the problem... ???? i mean how to write the __autoload() correctly? Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862462 Share on other sites More sharing options...
dimjaxor Posted June 24, 2009 Share Posted June 24, 2009 function __autoload($class) { require_once("CloneDemo1.class.php"); } that is how you include a file. require_once("path/and/filename/of/file/to/be/included.php") Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862663 Share on other sites More sharing options...
KevinM1 Posted June 24, 2009 Share Posted June 24, 2009 For more on __autoload: http://www.phpfreaks.com/forums/index.php/topic,257613.msg1211970.html#msg1211970 Quote Link to comment https://forums.phpfreaks.com/topic/163450-how-to-instantiate-one-class-in-another-class/#findComment-862666 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.