Jump to content
  • Advertisement
Sign in to follow this  
Smit

[web] PHP Classes?

This topic is 4617 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
Advertisement
You can declare a member using var $member; (note the keyword). You can then initialize that member in a constructor.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!