PHP: Notice: Trying to get property of non-object -
i ran next error: notice: trying property of non-object in c:\xampp\htdocs\time\app\classes\radiostatus.class.php on line 42
, couldn't find answer.
the total error line 42 till 45. these lines:
$this->data->setdata('stream_status', $this->simplexml->streamstatus); $this->data->setdata('stream_titel', $this->simplexml->servertitle); $this->data->setdata('stream_luisteraars', $this->simplexml->currentlisteners); $this->data->setdata('stream_huidignummer', $this->simplexml->songtitle);
the $this->data
thing class, passed through constructor. because i'm using depency injections
. how info class works (pretty basic):
class info { private $data = array(); function __construct() { $this->setdata('naam', 'time'); $this->setdata('slogan', 'the time has come'); } public function setdata($key, $value) { homecoming $this->data[$key] = $value; } public function getdata($key) { homecoming $this->data[$key]; } }
as can see, setdata
function exists. still, i'm getting darn errors. please help me find problem these errors, , if more code needed, please so. edit:
the radiostatus class:
class rafiostatus { function __construct($data) { $this->data = $data; curl_setopt(curl_init(), curlopt_url, '178.32.13.195:8004/admin.cgi?sid=1&mode=viewxml&page=1'); curl_setopt(curl_init(), curlopt_header, false); curl_setopt(curl_init(), curlopt_returntransfer, true); curl_setopt(curl_init(), curlopt_post, false); curl_setopt(curl_init(), curlopt_httpauth, curlauth_basic); curl_setopt(curl_init(), curlopt_userpwd, 'admin:assembla12'); curl_setopt(curl_init(), curlopt_followlocation, true); curl_setopt(curl_init(), curlopt_useragent, $_server['http_user_agent']); $this->xml = curl_exec(curl_init()); curl_close(curl_init()); $this->simplexml = simplexml_load_string($this->xml); $this->setdata('stream_status', $this->simplexml->streamstatus); $this->setdata('stream_titel', $this->simplexml->servertitle); $this->data->setdata('stream_luisteraars', $this->simplexml->currentlisteners); $this->data->setdata('stream_huidignummer', $this->simplexml->songtitle); } }
please validate parameter before trying manipulate it:
<?php class radiostatus { function __construct($data) { if (!($data instanceof data)) { throw new \invalidargumentexception('$data not valid'); } // safe now... $this->data = $data; } }
you utilize type hinting in case:
function __construct(data $data) { /** code... */ }
php
No comments:
Post a Comment