Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/205937-class-nightmare/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/205937-class-nightmare/#findComment-1077625
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/205937-class-nightmare/#findComment-1077683
Share on other sites

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.