Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question How to create a global variable of type ?.class.php [PHP5]

    I'm trying to create a global variable ($db) of type Database (as shown below) to be accessed within Tools class.

    I'm using PHP5 and running into a little problem... I have 2 files:
    - Database.class.php
    - Tools.class.php

    The contents look as follows:
    Code:
    <?php
    require 'Database.class.php';
    
    class Tools
    {
        public  $db       = "";	// is this the problem?
    
        function Tools($server, $user, $password, $database)
        {
            $db = new Database($server, $user, $password, $database);
        }
        
        public function Run()
        {
    	$db->Connect();
        }
    }
    ?>
    Code:
    <?php
    class Database
    {
        function Database($server, $user, $password, $database)
        {
            // DO SOMETHING HERE //
        }
    
        public function Connect()
        {
            // DO SOMETHING HERE //
        }
    For some odd reason this doesn't seem to work ... When I am in Tools.class.php I am trying to access the Database public variable ($db) to run ($db->Connect()) as follows:
    $db->Connect();
    But ... as you may have guessed ... this doesn't work...

    Any clues/hints would be much appreciated.
    Thanks,

  • #2
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Try this:

    PHP Code:
    <?php
    require 'Database.class.php';

    class 
    Tools
    {
        private 
    $db NULL;

        function 
    Tools($server$user$password$database)
        {
            
    // Use $this to access class methods and properties
            
    $this->db = new Database($server$user$password$database);
        }
        
        public function 
    Run()
        {
           
    $this->db->Connect();
        }
    }
    ?>
    You can also extend database class to use its methods directly

    PHP Code:
    <?php
    require 'Database.class.php';

    class 
    Tools extends Database
    {
         private 
    $db NULL;

        function 
    Tools()
        {
           ...
        }
        
        function 
    get_data($id)
        {
             
    // access Database class method
             
    $this->get_row_by_id($id);
        }
    }
    ?>
    Last edited by steelaz; 03-25-2009 at 03:54 AM.

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post
    steelaz: Sweet, that worked - but I'm having another semi-similar problem...

    Now I have an index.php which looks as follows:
    Code:
            <?php
            require 'Tools/Tools.class.php';
            
            $tool = new Tools();
            $tool->db->Connect();
            ?>
    This doesn't seem to work, I do $tool-> and I get "db" as an option (so far so good) but then -> doesn't give me connect ... In my example seeing as $db is PUBLIC in tools why can't I do $tool->db->Connection(); within index.php?

    Thanks a lot...

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Don't worry about what you can't see. PHP is a datatype weak language and cannot determine the type outside of runtime.
    If you're using eclipse for example, you can add this above the $db value:
    PHP Code:
    /**
     * @var Database
     */
    public $db
    This will let the autocomplete in eclipse (or hopefully whatever you're using) realize that $tools->db is a typeof Database, and can use anything from database.
    If you just did $tools->db->connect(); it should still work, you just won't see the autocomplete.
    I wouldn't actually let these by public. Its a pain to write an accessor for it, but at least the value cannot be overwritten.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You'll also note (semi-unrelated) that PHP5 constructors are always named __construct() {}.

    The naming method you are using still works purely for the sake of compatibility for older code.

  • #6
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post
    Fou-Lu: Elabortate on "an accessor"? Somethint that would expose all the functionality of $db ...? how would I go about doing that?

    Thanks,

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Accessors / mutators are just methods to handle access to private variables. Generally speaking, they do not reveal the actual underlying variable, but rather perform some processing on it otherwise. In this example, this approach wouldn't be feasible.
    This is what I mean:
    PHP Code:

    class Tools
    {
        private 
    $db;
        public function 
    getDB()
        {
            return 
    $this->db// This is actually a bad idea, I'll explain here
        
    }

    Now, the threat you have is that in PHP5 objects are always pass by reference which is different from the PHP 4 implementation where objects were passed by value. So, now you could do this:
    PHP Code:
    $connection = new Database();
    $conn2 = new Database2();

    $tools = new Tools($connection);
    $tmpDB $tools->getDB();
    $tmpDB $conn2
    Untested. In other languages what should happen is that the value of $db inside of Tools object is now compromised. Even though there is no setter you're returning a pointer to a database which should be overwritable. This is called a privacy leak.

    I would probably consider generating this as a design pattern that exposes the database in Tools as a typeof Database. Something like this:
    PHP Code:
    interface IDatabase
    {
        public function 
    connect(...);
        public function 
    select(...);
        ....
    }

    class 
    Database implements IDatabase
    {
        ....
    }

    class 
    Tools implements IDatabase
    {
        private 
    $db;
        public function 
    __construct(IDatabase $db)
        {
            
    $this->db $db;
        }

        public function 
    connect(...)
        {
        }
    }

    $connection = new Database(...);
    $tools = new Tools($connection);

    $tools->connect(...); 
    Something like this. This prevents you're leaking problem, but inherits a possible tight coupling problem. This is one of the things you need to really balance out when you're doing OOP to determine what best suits you're needs.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •