SchweppesAle Posted August 31, 2009 Share Posted August 31, 2009 Hi, I was trying my hand at OOP by converting some code I had already written. It's currently returning the following error message: "Using $this when not in object context on line 36" -- within the streamsend.php file streamsend.php <?php require_once('config.php'); class streamsend extends config{ var $_streamsend; var $_result; public function add($users, $num_entries) { $username = $_username; $password = $_password; $listid = $_listid; // Build the XML for the Request(s) for($i = 0; $i < $num_entries; $i++) { $request[] = "<system>". "<action>addsubscriber</action>". "<authorization>". "<username>$username</username>". "<password>$password</password>". "</authorization>". "<parameterlist>". "<parameter id='List ID'><value>$listid</value></parameter>". "[<parameter id='Auto-Activate'><value>true</value></parameter>]". "<parameterarr>". "<parameter id='{emailaddress}'><value>".$users[$i][1]."</value></parameter>". "<parameter id= '{firstname}'><value>".$users[$i][2]."</value></parameter>". "<parameter id= '{lastname}'><value>".$users[$i][3]."</value></parameter>". "<parameter id= '{title}'><value>".$users[$i][4]."</value></parameter>". "</parameterarr>". "</parameterlist>". "</system>"; } return $this->query($request); } public function query($request) { foreach($request as $entry) { // Configuration $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; $url = "http://server1.streamsend.com/streamsend/api.php"; // Build Parameters $params['xmldata'] = $entry; // Open Connection Handle $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$params); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // Send Request $response = curl_exec ($ch); // Close Connection Handle curl_close ($ch); $xml = simplexml_load_string($response); $return[] = $xml -> resultlist -> result[1]; } /* return $this->_result[] = $return;*/ return $return; } } ?> config.php <?php class config{ private static $_username = 'somename'; private static $_password = 'somepassword'; private static $_listid = '23523'; } ?> interface.php <?php require_once('connect.php'); require_once('streamsend.php'); $connect_db = new connect(); $connection = $connect_db->_connection; $users = $connect_db->_users; $num_entries = count($users); $streamsend = new streamsend(); echo var_dump(streamsend::add($users,$num_entries)); ?> Also, I was hoping for a few pointers if you guys have any regarding "The Best Way" of doing this(encapsulation through inheritence?, etc). I'm still a rookie programmer Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted August 31, 2009 Author Share Posted August 31, 2009 Just figured out what the problem was return streamsend::query($request); Would still appreciate any advice you guys could give me though. Really trying to stray away from functional programming so that I can work on a more professional level. Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted August 31, 2009 Author Share Posted August 31, 2009 Scratch that. The config class was actually passing NULL values for the private virtual methods (username, password). I've redone everything, however I feel like placing the values within a public constructor is kind of defeating the purpose. How would I go about doing this? <?php class config{ private $_username; private $_password; private $_listid; function __construct(){ $this->_username = 'someusername'; $this->_password = 'somepassword'; $this->_listid = '23532'; } function get_username() { return $this->_username; } function get_password() { return $this->_password; } function get_listid() { return $this->_listid; } } ?> streamsend.php <?php require_once('config.php'); class streamsend extends config{ function __construct(){ parent::__construct(); } public function list_users() { $username = $_username; $password = $_password; $listid = $_listid; $request = "<system> <authorization> <username>$username</username> <password>$password</password> </authorization> <action>getlists</action> </system>"; } public function add($users, $config) { $login = $config->get_username(); $password = $config->get_password(); $listid = $config->get_listid(); $num_entries = count($users); // Build the XML for the Request(s) for($i = 0; $i < $num_entries; $i++) { $request[] = "<system>". "<action>addsubscriber</action>". "<authorization>". "<username>".$login."</username>". "<password>".$password."</password>". "</authorization>". "<parameterlist>". "<parameter id='List ID'><value>$listid</value></parameter>". "[<parameter id='Auto-Activate'><value>true</value></parameter>]". "<parameterarr>". "<parameter id='{emailaddress}'><value>".$users[$i][1]."</value></parameter>". "<parameter id= '{firstname}'><value>".$users[$i][2]."</value></parameter>". "<parameter id= '{lastname}'><value>".$users[$i][3]."</value></parameter>". "<parameter id= '{title}'><value>".$users[$i][4]."</value></parameter>". "</parameterarr>". "</parameterlist>". "</system>"; } return streamsend::query($request); } public function query($request) { foreach($request as $entry) { // Configuration $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; $url = "http://server1.streamsend.com/streamsend/api.php"; // Build Parameters $params['xmldata'] = $entry; // Open Connection Handle $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$params); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // Send Request $response = curl_exec ($ch); // Close Connection Handle curl_close ($ch); $xml = simplexml_load_string($response); $return[] = $xml -> resultlist -> result[1]; } return $return; } } ?> interface.php <?php require_once('connect.php'); require_once('streamsend.php'); require_once('config.php'); $connect_db = new connect(); $streamsend = new config; $connection = $connect_db->_connection; $users = $connect_db->_users; echo var_dump(streamsend::add($users, $streamsend)); ?> Another problem that I noticed is under the function add() within the streamsend.php file. I had previously tried creating an object from the streamsend class hoping that it would inherit all virtual variables from the config.class. ex: within streamsend::add() streamsend = new streamsend; However, even after including parent::__construct(); within the streamsend.php; I found that this was not the case. (variables where returning NULL which tells me the construct within config.php wasn't being executed). Am I missing something obvious? 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.