The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsI'm writing a PHP engine that is supposed to work like TinyURL (a url shortener), and I just got the pages up for testing. Of course, I got an error in this test. The problem is that I can't figure it out.
Here is the page:
http://url.knoxius.com/redirect.php?id=3 4534
Here is the error:
Fatal error: Access to undeclared static property: url::$id in /home/knoxius8/public_html/apps/url/class/url.php on line 35
In the class, I have a constructor that is supposed to set the a property in the parent object to the value, but apparently it's not working. Here is the (shortened) code:
class url {
public $id;
public $url;
public $timestamp;
public $ip;
public $password;
public $referrer;
//Class Construct
public function __construct() {
$this->timestamp = time();
$this->ip = md5($_SERVER['REMOTE_ADDR']);
$this->referrer = $this->clear($_SERVER['HTTP_REFERER']);
//the clear() method i'm calling here is in the full script, but i edited it out for this post
}
}
/************ Redirect to URL Class ************/
class url_redirect extends url {
//Redirection Class Construct
public function __construct($id) {
parent::$id = intval($id);
}
}
Also, here is the code that calls to these objects (again, shortened):
$id = $_GET['id'];
$url = new url();
$redirect = new url_redirect($id);
I really don't see any errors in these scripts...but then again I'm tired, and fairly new to PHP OOP. Any help would be appreciated.
wat
a compessor wil raise the volume while lowering the db - chronamut
As a quick test try this
$id2 = $_GET['id'];
$url = new url();
$redirect = new url_redirect($id2);
It just looks like you could be clashing with the $id variable you use in the url class.
You have made a mistake in your OOP logic.
Your class url_redirect already inherited every method and variable from url. Think about it this way: everything from url is already inside of url_redirect.
So when you say
parent::$id = intval($id);
PHP doesn't know what to do with that. Why are we trying to set the value of a variable in the parent class? The parent class isn't an object - you can't change any data inside it. The only thing he could be trying to do is call a static variable, but $id isn't static.
So the proper way to set $id is to just do this:
class url_redirect extends url {
public function __construct($id) {
$this->id = intval($id);
}
public function test() {
echo $this->id;
}
}
$redirect = new url_redirect(3);
$redirect->test();//outputs 3
~fourthfrench