Jump to content

New to OOP: Why can't I create a new instance from within a function?


Jack.Straw

Recommended Posts

I've been reading about OOP, and I think i've got a basic grasp on the consept.  However, I still can't get the code below to work from within a function.  Can anyone help me understand why?

The script below is used to get shipping rates from UPS.

This version works perfectly:
[code]
include 'incUPS.php';
$userid = "MyUserID";
$userid_pass = "password";
$access_key = "MyKey";
$activity = "activity";
$length = 10;
$width = 10;
$height = 10;
$to_zip = '32801';
$from_zip = '63144';
$weight = 10;
$service_code = '03';

$y = "<?xml version=\"1.0\"?><AccessRequest xml:lang=\"en-US\"><AccessLicenseNumber>$access_key</AccessLicenseNumber><UserId>$userid</UserId><Password>$userid_pass</Password></AccessRequest><?xml version=\"1.0\"?><RatingServiceSelectionRequest xml:lang=\"en-US\"><Request><TransactionReference><CustomerContext>Bare Bones Rate Request</CustomerContext><XpciVersion>1.0</XpciVersion></TransactionReference><RequestAction>Rate</RequestAction><RequestOption>Rate</RequestOption></Request><PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></Shipper><ShipTo><Address><PostalCode>$to_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipTo><ShipFrom><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipFrom><Service><Code>$service_code</Code></Service><Package><PackagingType><Code>02</Code></PackagingType><Dimensions><UnitOfMeasurement><Code>IN</Code></UnitOfMeasurement><Length>$length</Length><Width>$width</Width><Height>$height</Height></Dimensions><PackageWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>$weight</Weight></PackageWeight></Package></Shipment></RatingServiceSelectionRequest>";
// cURL ENGINE
$ch = curl_init(); /// initialize a cURL session
curl_setopt ($ch, CURLOPT_URL, "https://www.ups.com/ups.app/xml/Rate");
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$y");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xyz = curl_exec ($ch);
curl_close ($ch);
$obj = new xml($xyz, "xml");
$UPSShipCost = $xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[0];
echo "UPS Standard Ground: $UPSShipCost";
[/code]

However, as soon as i place it in a function it stops working:
[code]
include 'incUPS.php';
GetRate();

function GetRate()
{
$userid = "MetroLighting";
$userid_pass = "metro1";
$access_key = "ABFD16FF7BA0CA76";
$activity = "activity";
$length = 10;
$width = 10;
$height = 10;
$to_zip = '32801';
$from_zip = '63144';
$weight = 10;
$service_code = '03';

$y = "<?xml version=\"1.0\"?><AccessRequest xml:lang=\"en-US\"><AccessLicenseNumber>$access_key</AccessLicenseNumber><UserId>$userid</UserId><Password>$userid_pass</Password></AccessRequest><?xml version=\"1.0\"?><RatingServiceSelectionRequest xml:lang=\"en-US\"><Request><TransactionReference><CustomerContext>Bare Bones Rate Request</CustomerContext><XpciVersion>1.0</XpciVersion></TransactionReference><RequestAction>Rate</RequestAction><RequestOption>Rate</RequestOption></Request><PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></Shipper><ShipTo><Address><PostalCode>$to_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipTo><ShipFrom><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipFrom><Service><Code>$service_code</Code></Service><Package><PackagingType><Code>02</Code></PackagingType><Dimensions><UnitOfMeasurement><Code>IN</Code></UnitOfMeasurement><Length>$length</Length><Width>$width</Width><Height>$height</Height></Dimensions><PackageWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>$weight</Weight></PackageWeight></Package></Shipment></RatingServiceSelectionRequest>";
// cURL ENGINE
$ch = curl_init(); /// initialize a cURL session
curl_setopt ($ch, CURLOPT_URL, "https://www.ups.com/ups.app/xml/Rate");
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$y");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xyz = curl_exec ($ch);
curl_close ($ch);
$obj = new xml($xyz, "xml");
$UPSShipCost = $xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[0];
echo "UPS Standard Ground: $UPSShipCost";
}
[/code]

For reference, here is the included file containing the classes:
[code]
class xml_container {

    function store($k, $v)
    {
        $this-> {
            $k}
        [] = $v;
    }
}

class xml {
    var $current_tag = array();
    var $xml_parser;
    var $Version = 1.0;
    var $tagtracker = array();

    function startElement($parser, $name, $attrs)
    {
        array_push($this->current_tag, $name);
        $curtag = implode("_", $this->current_tag);
        if (isset($this->tagtracker["$curtag"])) {
            $this->tagtracker["$curtag"]++;
        } else {
            $this->tagtracker["$curtag"] = 0;
        }
        if (count($attrs) > 0) {
            $j = $this->tagtracker["$curtag"];
            if (!$j) $j = 0;
            if (!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
                $GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
            }
            $GLOBALS[$this->identifier]["$curtag"][$j]->store("attributes", $attrs);
        }
    }

    function endElement($parser, $name)
    {
        $curtag = implode("_", $this->current_tag); // piece together tag
        if (!$this->tagdata["$curtag"]) {
            $popped = array_pop($this->current_tag); // or else we screw up where we are
            return; // if we have no data for the tag
        } else {
            $TD = $this->tagdata["$curtag"];
            unset($this->tagdata["$curtag"]);
        }
        $popped = array_pop($this->current_tag);
        if (sizeof($this->current_tag) == 0) return;
        $curtag = implode("_", $this->current_tag);
        $j = $this->tagtracker["$curtag"];
        if (!$j) $j = 0;
        if (!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
            $GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
        }
        $GLOBALS[$this->identifier]["$curtag"][$j]->store($name, $TD); #$this->tagdata["$curtag"]);
        unset($TD);
        return true;
    }

    function characterData($parser, $cdata)
    {
        $curtag = implode("_", $this->current_tag);
        $this->tagdata["$curtag"] .= $cdata;
    }

    function xml($data, $identifier)
    {
        $this->identifier = $identifier;
        $this->xml_parser = xml_parser_create();
        xml_set_object($this->xml_parser, $this);
        xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($this->xml_parser, "characterData");
        if (!xml_parse($this->xml_parser, $data, true)) {
            sprintf("XML error: %s at line %d",
                xml_error_string(xml_get_error_code($this->xml_parser)),
                xml_get_current_line_number($this->xml_parser));
        }
        xml_parser_free($this->xml_parser);
    }
}
[/code]


Thanks in advance for any insight you can offer!
-Jack
[quote author=makeshift_theory link=topic=117863.msg481182#msg481182 date=1165594993]
Well here is something:
[code]$obj = new xml($xyz, "xml");[/code]

You have a new object and your passing variables to use with the constructor but yet you don't have a construct function.
[/quote]

Isn't this the contruct function?:
[code]
    function xml($data, $identifier)
    {
        $this->identifier = $identifier;
        $this->xml_parser = xml_parser_create();
        xml_set_object($this->xml_parser, $this);
        xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($this->xml_parser, "characterData");
        if (!xml_parse($this->xml_parser, $data, true)) {
            sprintf("XML error: %s at line %d",
                xml_error_string(xml_get_error_code($this->xml_parser)),
                xml_get_current_line_number($this->xml_parser));
        }
        xml_parser_free($this->xml_parser);
    }
[/code]
Well, i'm not entirely sure honestly, but i *think* it is taken from an XML returned by the UPS server.  See, the script creates and XML request for a shipping price, sends it to UPS, then outputs the returned price.  I think that crazy looking xml variable is filled within the class, using a value taken from the returned XML from UPS.

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.