Jack.Straw Posted December 8, 2006 Share Posted December 8, 2006 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 sessioncurl_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 sessioncurl_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 Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/ Share on other sites More sharing options...
makeshift_theory Posted December 8, 2006 Share Posted December 8, 2006 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. Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137553 Share on other sites More sharing options...
Jack.Straw Posted December 8, 2006 Author Share Posted December 8, 2006 [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] Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137569 Share on other sites More sharing options...
makeshift_theory Posted December 8, 2006 Share Posted December 8, 2006 Yes I overlooked the function name sorry about that ;D. Are you receiving any errors? Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137580 Share on other sites More sharing options...
Jack.Straw Posted December 8, 2006 Author Share Posted December 8, 2006 Nope, no errors. The first version of the script i posted displays:[code]UPS Standard Ground: 6.45[/code]The second displays:[code]UPS Standard Ground:[/code]No error.log is created for either. I'm baffled... Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137582 Share on other sites More sharing options...
makeshift_theory Posted December 8, 2006 Share Posted December 8, 2006 $UPSShipCost = $xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[0];where is the $xml variable coming from? The class? Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137584 Share on other sites More sharing options...
Jack.Straw Posted December 8, 2006 Author Share Posted December 8, 2006 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. Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137588 Share on other sites More sharing options...
Jack.Straw Posted December 8, 2006 Author Share Posted December 8, 2006 Anyone who cares to play with or test the script, feel free to enter the following information at the top to make it functional:$userid = "Jack-Straw";$userid_pass = "123456";$access_key = "FBFD2BAA6CD88F74"; Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137597 Share on other sites More sharing options...
makeshift_theory Posted December 8, 2006 Share Posted December 8, 2006 Well that part is the problem with your script, I'm not sure where that variable is coming from but I can see it's calling a object and I don't see that array in your class. Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137619 Share on other sites More sharing options...
Jack.Straw Posted December 8, 2006 Author Share Posted December 8, 2006 But why would it work outside the confine's of a function and not within? ??? Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-137621 Share on other sites More sharing options...
Jack.Straw Posted December 9, 2006 Author Share Posted December 9, 2006 bump for more input. Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-138038 Share on other sites More sharing options...
Jack.Straw Posted December 11, 2006 Author Share Posted December 11, 2006 ^ Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-138809 Share on other sites More sharing options...
Jack.Straw Posted December 11, 2006 Author Share Posted December 11, 2006 Last bump... can anyone help? Link to comment https://forums.phpfreaks.com/topic/29939-new-to-oop-why-cant-i-create-a-new-instance-from-within-a-function/#findComment-139195 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.