HGeneAnthony Posted May 30, 2006 Share Posted May 30, 2006 I was wondering how can I create an instance of my class inside another class? Example:[code] class Auction { require("search.php"); var $search = new Search(); $search->setAuctionMonth(05); $search->setAuctionDay(15); $search->setAuctionYear(2006); $search->setOrderBy(""); $search->setMake("ford"); $search->setModel(""); $search->setYearLow(""); $search->setYearHigh(""); $search->setMilesLow(""); $search->setMilesHigh(""); }[/code]This shoots out an error. How can I write this so I can call my other class from inside my current one. Quote Link to comment https://forums.phpfreaks.com/topic/10754-how-can-i-create-a-class-instance-from-another-class/ Share on other sites More sharing options...
poirot Posted May 30, 2006 Share Posted May 30, 2006 I don't think you can do that. But you can use the statement [i]extends[/i] statement to "reuse" a class:[a href=\"http://www.php.net/manual/en/language.oop.php\" target=\"_blank\"]http://www.php.net/manual/en/language.oop.php[/a][a href=\"http://www.php.net/manual/en/keyword.extends.php\" target=\"_blank\"]http://www.php.net/manual/en/keyword.extends.php[/a] Quote Link to comment https://forums.phpfreaks.com/topic/10754-how-can-i-create-a-class-instance-from-another-class/#findComment-40172 Share on other sites More sharing options...
Zane Posted May 30, 2006 Share Posted May 30, 2006 set the require statement outside of the class, at the top of the pagethen just call it like you would any other Class[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]But you can use the statement extends statement to "reuse" a class:[/quote]not a good ideayou might not want one class to have the properties of anotheronly use extends when you need to .....literally extend from itfor instance if you made a Car classand you decided to make a Motorcycle class also....then you could logically EXTEND it onto CAR, maybe Quote Link to comment https://forums.phpfreaks.com/topic/10754-how-can-i-create-a-class-instance-from-another-class/#findComment-40190 Share on other sites More sharing options...
poirot Posted May 30, 2006 Share Posted May 30, 2006 [!--quoteo(post=378324:date=May 29 2006, 11:58 PM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ May 29 2006, 11:58 PM) [snapback]378324[/snapback][/div][div class=\'quotemain\'][!--quotec--]set the require statement outside of the class, at the top of the pagethen just call it like you would any other Classnot a good idea[/quote]zanus, maybe I am doing it wrong, but no matter where you put the require statement, once you try to initiate the class it will throw you an error like this:[b]Parse error: parse error, unexpected T_NEW in...[/b] Quote Link to comment https://forums.phpfreaks.com/topic/10754-how-can-i-create-a-class-instance-from-another-class/#findComment-40234 Share on other sites More sharing options...
HGeneAnthony Posted May 31, 2006 Author Share Posted May 31, 2006 Well I found out that the require statement must be outside of a class (at the to preferably). I found out you can create have an instance of a class inside a class however you need to specify the variable at the top of the class:var $cars;and instantiate it inside a method:$cars = new Cars();This would work!Extending a class is generally only used for an abstract class like human and dog would extend mammal. You would put all the common characteristics between humans and dogs inside of mammal and then extend the human and dog off of them. You can even override characteristics of mammal inside of human or dog. This keeps your code cleanier (hence less error prone) and builds on a modular design which is more better for designing any project. Another use for extending classes is so that you don't have to modify a very modular class with project specific code. You design classes that can be dumped in everything and then create an extended class on top of it to modify anything specific to your project. Quote Link to comment https://forums.phpfreaks.com/topic/10754-how-can-i-create-a-class-instance-from-another-class/#findComment-40435 Share on other sites More sharing options...
trq Posted May 31, 2006 Share Posted May 31, 2006 You can use the require() within a class, however, it also needs to be within a method. eg;[code]class Auction { function createSearch() { require "search.php"; return new Search(); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10754-how-can-i-create-a-class-instance-from-another-class/#findComment-40437 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.