Page 1 of 1

Help with POST vars inside class

Posted: Mon May 12, 2003 5:33 pm
by nigma
Hey,

I have a form that submits itself to the php script on the same page. Inside the php script, there is a class called mysql, and I am wondering why this is not working.

I have this(inside the class):

Code: Select all

var $database = $_POSTї'database'];
var $table 	= $_POSTї'table'];
var $width	= $_POSTї'width'];
now it says the error is around this area, but I dont know what the problem is.

Thanks for any help.

Posted: Mon May 12, 2003 7:52 pm
by volka
http://www.php.net/manual/en/language.oop.php
Note: In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).

Code: Select all

class MyClass
{ // won't work
	var $database = $_POSTї'database'];
	var $table = $_POSTї'table'];
	var $width = $_POSTї'width'];
	...
}

Code: Select all

class MyClass
{ // this should	
	var $database;
	var $table;
	var $width;
	
	function MyClass()
	{
		$this->database = $_POST['database'];
		$this->table = $_POST['table'];
		$this->width = $_POST['width'];
	}
...
}
hope this helps.

Posted: Mon May 12, 2003 8:57 pm
by McGruff
It could be that it makes life simpler in your script if you declare class variables for each of the POST vars, but you could just access them directly with $_POST['varname'] wherever they're needed - saves the overhead of creating extra vars.

You maybe didn't post all your code: would you need to do some checks in the constructor to prevent hackers accessing any old table or database?