Jump to content

[SOLVED] Class within a class


SetToLoki

Recommended Posts

hi I am creating a new class and I want to use my previously connected mysql class inside of it, being able to reference the mysql inside the new class I am creating.

 

example of what I tried to do

 

<?php
class user {
 private $user = "";
 private $userlevel = "";

 private $db;
 public function _construct()
 {
 	global $db_host, $db_user, $db_pass, $db_name;
 	$this->db = new MySQL($db_host, $db_user, $db_pass, $db_name);
 }

        function test()
        {
                $sql = "SOME SQL CODE";
                $results = $this->db->query($sql);
 	$rows = $this->db->fetch_assoc($results);	
        } 
}
?>

Link to comment
Share on other sites

Hey,

 

global $db_host, $db_user, $db_pass, $db_name;

 

Instead of setting the variables as global, you should pass them, in this case, via the constructor for use with the MySQL class.

 

You didn't state if there was a problem.

Link to comment
Share on other sites

Actually, just saw it,

 

_construct()

 

For the constructor to run automatically on instantiation you require two underscores;

 

__construct()

 

how embarrassing for me :(, sometimes I guess just look to hard and make it all complex when the answer is obvious, thanks my good man/lady!

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.