Sign in to follow this  

[web] PHP Classes?

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a class for interacting with a mysql database which is in its own file. I have another file in which I declare another class and require_once() the mysql file at the top, however if I try to do $db = new MySQLDB; it errors. If its outside of the class its works.
require_once("mysql.php");

$db = new MySQLDB; // # This Works.

class NewClass()
{
  $insideclassdb = new MySQLDB; // # This fails.

  function SomeFunction()
  {
     // Do stuff here.
  }
};

The error is: Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION How can you use a class inside of another class?

Share this post


Link to post
Share on other sites
Like this?


require_once( "mysql.php" );

class CUsers
{
var $database;

function CUsers( )
{
$database = new CMySQL;
}

function AddUser( $username, $password, $email, $name )
{
$query = "INSERT INTO ix_users VALUES( 0, 1, '$username', '$name', '$email', MD5('$password'), 0, 0, 0, 0 )";
$database->Query( $query );
}
};



It just says that $database is not an object and therefor can't call Query().

Share this post


Link to post
Share on other sites
$database is a local variable. The member variable of the class is acessed with $this->database.

Also, you can't chain this syntax, so you would have to do:
$database = & $this->database;
$database->Query( );


Share this post


Link to post
Share on other sites

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this