N-Bomb(Nerd) Posted June 26, 2010 Share Posted June 26, 2010 Hi, I'm trying to use multiple classes in my project. I've got a situation where I've already made an instance of a class and have passed variables into it. I've been trying to figure out a way to use that same instance inside of another class of mine without resetting the values I've already previously set. Here's a rough example--- main.php // login to the website by selecting the profile passed in $curl = new curl('N-Bomb(Nerd)'); // do a function in the curl class that pulls more information $curl->gather_information(); $auction = new auction(); // From my trial and error I've found that using extend on this class resets any information I have.. // I need to be able to access the information that's already in $curl inside of the auction class $latest_auction = $auction->get_latest('123'); // should return the latest auction because we're logged in as "N-Bomb(Nerd)" curl.class.php class curl { private $username; private $information; public function __construct($username) { $this->username = $username; // do other stuff // make sure the profile exists // login to website using the profile of $username // do whatever } } auction.php class auction { private $var; //.. whatever code you want to apply here public function get_latest($auction_id) { // want to use the existing instance of curl here // to pull the information from the url // that way it's still logged in as the username we wanted // without having to say which user again $curl->get_page('http://example.com/auction.php?id=' . $auction_id); } } Basically, is there any way to make my instances global and accessible to everything without having to pass the instance in every time? Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted June 26, 2010 Share Posted June 26, 2010 yes. this is a rough example: class first { public $name; function __construct(){ $this->name = "it works!"; } } class second { public $other; public function go(){ echo $this->other->name; } } ////////heres the rest: $first = new first(); $second = new second(); $second->other = $first; $second->go(); basically, you're storing that other instance in a global variable of another instance Quote Link to comment Share on other sites More sharing options...
ignace Posted June 26, 2010 Share Posted June 26, 2010 Your design is flawed. cURL has no direct relation to your Auction besides that cURL provides the service it provides (reading the HTML source, for which other tools are available). In the below example DomDocument provides the reading service, AuctionDAO does his part by parsing the source and translate it into an array. AuctionRepository stores an array of Auction's and facilitates data iteration. Auction is the representation of an auction. class Auction { // auction specific details } class AuctionRepository { private $auctions = array(); public function __construct( $auctions = array() ) { $this->addAll( $auctions ); } public function addAll( $auctions ) { foreach( $auctions as $auction ) { $this->add( $auction ); } } } class AuctionDAO { public function findById( $auctionID ) { $auctions = array(); if( $this->dom->loadHtmlFile( 'http://example.com/auction.php?id=' . urlencode( $auctionID ))) { $xpath = new DomXPath( $this->dom ); foreach( $xpath->query( '//auction' ) as $auction ) { $details = array(); foreach( $auction->getChildren() as $tag => $value ) { $details[$tag] = $value; } $auctions[] = $details; } } return $auctions; } } Explain what you are trying to do and what you want to achieve and we can further assist you. Quote Link to comment 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.