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 3 of 3

Thread: php classes

  1. #1
    Regular Coder slappyjaw's Avatar
    Join Date
    Mar 2009
    Location
    Wonderland!
    Posts
    146
    Thanks
    14
    Thanked 3 Times in 3 Posts

    Question php classes

    Currently i have been working on this project for a networking script and i am almost done with the class that i am making for it. The thing is is that the thing is huge and all the functions for the site are in that class, is there a way to break it up so that i can have one class that calls it and other classes that branch off of that class that are accessible by the main class?! for example
    PHP Code:
    // The main class
    class main{
    function 
    hello(){
    echo 
    "hello!";
    }
    }

    // Then the sub classes
    class sub{
    function 
    welcome(){
    echo 
    "welcome";
    }

    class 
    sub2{
    function 
    bye(){
    echo 
    "bye.";
    }

    // Now when i call the class can i make it so that i can do it like this.

    $main = new main();
    $main->hello();
    $main->welcome();
    $main->bye();
    }

    Thans for any help in advance!

  • #2
    Regular Coder slappyjaw's Avatar
    Join Date
    Mar 2009
    Location
    Wonderland!
    Posts
    146
    Thanks
    14
    Thanked 3 Times in 3 Posts
    ahhh never mind i took a looksy at how smarty does their business in their classes.

  • #3
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    you can use pattern Singleton
    PHP Code:
    class main {
            public 
    $sub;
            public 
    $sub2;
            static private 
    $instance;
            
            private function 
    __construct() {}
            
            private function 
    __clone() {}
            
            static function 
    getInstance() {
                if(
    self::$instance == NULL) {
                    
    self::$instance = new self();
                    
    self::$instance->Init();
                }
                return 
    self::$instance;
            }
            
            public function 
    Init() {
                
    $this->sub self::getInstance();
                
    $this->sub2 = new gfUsers();
            }
            
            public function 
    hello(){
                echo 
    "hello!";
            }
    }

    class 
    sub {
            function 
    __construct() {
                
    $this->main main::getInstance();
            }
            
            function 
    welcome(){
                echo 
    "welcome";
            }
    }

    class 
    sub2 {
            function 
    __construct() {
                
    $this->main main::getInstance();
            }
            
            function 
    bye(){
                echo 
    "bye.";
            }
            
            function 
    hello2() {
                
    $this->mail->hello();
            }

    Now is magic:

    PHP Code:
    $main = new main();                  // init single class
    $main->hello();                         // prints 'hello'
    $main->sub->welcome();            // prints 'welcome'
    $main->sub2->bye();                 // prints 'bye'
    $main->sub2->hello2();              // prints 'hello' 
    Or you can use simple inheritance
    Last edited by poyzn; 11-13-2010 at 07:20 AM.


  •  

    Posting Permissions

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