Help with POST vars inside class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Help with POST vars inside class

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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?
Post Reply