Jump to content

[PHP5] Class problems


Drezard

Recommended Posts

Im getting this error:

 

 

Parse error: syntax error, unexpected T_NEW in C:\xampp\htdocs\input.class.php on line 17

 

 

Heres the code for input.class.php:

 

<?php

require_once('database.class.php');

class input {

	private $database = new database;

	function input () {

		$this->database->moderator();

	}

}

?>

 

heres the code for database.class.php:

 

<?php

class database {

	// declare a few public variables
	private $database_host;
	private $database_username;
	private $database_password;
	private $database_database;
	private $database_connection;

	// connect to the database
	function connect() {

		$this->database_connection = mysql_connect($this->database_host, $this->database_username, $this->database_password);

		mysql_select_db($this->database_database, $this->database_connection);

	}

	// set up the database server constants
	function database () {

		$this->database_host = 'localhost';
		$this->database_database = 'alias';

	}

	// set up a moderator user, priveleges: SELECT, INSERT, UPDATE, DELETE
	// this user is used in the end-user app for anything more then just SELECT privelege job
	function moderator () {

		$this->database_username = 'moderator';
		$this->database_password = '******';

		$this->connect();

	}

	// set up a basic user, priveleges: SELECT
	// this user is used in the end-user app to do any SELECT jobs no more priveleges
	function user () {

		$this->database_username = 'user';
		$this->database_password = '******';

		$this->connect();

	}

	// set up a maintance user, priveleges: SELECT, INSERT, UPDATE, DELETE
	// this user is used for cron jobs and other maintance tasks
	// DANGER: should not be used in any end-user part of the app 
	function maintance () {

		$this->database_username = 'maintance';
		$this->database_password = '******';

		$this->connect();

	}

}

?>

 

Whats wrong with this?

 

Daniel

Link to comment
Share on other sites

You need to create the object in your constructor, i.e

<?php

require_once('database.class.php');

class input {

	private $database; //database object

	function input () {
	        $this->database = new database();
		$this->database->moderator();

	}

}

?>

Link to comment
Share on other sites

hi,

but the most important thing you are missing here is, Which I got at glance is

whenever you declear the object of any class it should be like this

 

$obj= new ClassName();//Class Name

if you notice your code carefully you are not using parenthesis with your class name when creating object

so

private $database = new database;//wrong approach

try this

private $database = new database();//correct one.

try out this! I am sure the parse error for new line will be eliminated.

Thanks!

Keep Smile

Numan Khan

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.