SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    public/private class?

    Can one define the scope of a class in PHP like one can in java? I want to create an instance of my DB_Sql class and make it accessible to all other classes.

    unregistered.

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is not currently in PHP4.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can one do it in php5? if this is not available in php4 then how do i make my class instance available to all other classes?

    unregistered.

  4. #4
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you mean something like this:
    PHP Code:
    <?php
    class MySQL {
        function 
    fake ($var) {
            echo 
    $var;
        }
    }
     
    class 
    Test {
        function 
    Test ($db) {
            
    $db->fake ('This is a test.');
        }
    }
     
    $db = &new MySQL;
    $test = &new Test ();
    ?>
    This will pass the MySQL class to the Test class and call a method on the MySQL class.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  5. #5
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ok... so the bottom line is that every object is in the global scopre the moment it is declared??

  6. #6
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Erm, no. You do have to explicitly pass the object to the new class. I think he just omitted that part:

    $db = &new MySQL;
    $test = &new Test ($db);

  7. #7
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, typo almost screwed everything up. Yes, Phil.Roberts is correct, you have to pass the MySQL object to the Test object.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  8. #8
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats what I thought. But passing the DB object to every class is a painfull task. Are there any other alternatives? Can I declare an object to be global? Would this global declaration be hindered with register_globals was turned of?

    unregistered.

  9. #9
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Globals variables are baaaad mmm'kay?

  10. #10
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In PHP 5, you can define public and private classes. You can also set the access for the member variables:

    PHP Code:
    public class Database
    {
       private 
    $linkId;
       protected 
    $dbName;
       public 
    $user;
       static 
    $calls;
     
       public function 
    connectDatabase $instance )
       {
          
    $instance->runQuery("select stuff");
       }

    As far as that. If that answers your question. There is really no substitute in PHP 4, at least not one as clean as this.
    Ryan

  11. #11
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to look into the Singleton pattern http://phppatterns.com/index.php/art...cleview/6/1/1/
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor


Bookmarks

Posting Permissions

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