Be a Supporter!

Access to Undeclared Property

  • 384 Views
  • 2 Replies
New Topic Respond to this Topic
Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Access to Undeclared Property 2009-08-29 07:42:38 Reply

I'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

Jon-86
Jon-86
  • Member since: Jan. 30, 2007
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Access to Undeclared Property 2009-08-29 08:06:40 Reply

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.


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik
irc.freenode.net #ngprogramming

BBS Signature
fourthfrench
fourthfrench
  • Member since: Aug. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Access to Undeclared Property 2009-08-29 15:16:27 Reply

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