How to create a global variable of type ?.class.php [PHP5]
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

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

  1. #1
    Join Date
    Oct 2004
    Posts
    429

    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. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,869

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

    Try using the following instead.

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

    class 
    Tools {
      public  
    $db "";

      function 
    Tools($server$user$password$database) {
        
    $this->$db = new Database($server$user$password$database);
      }
        
      public function 
    Run() {
        
    $this->$db->Connect();
      }
    }
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center