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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    Quick Class Question

    Hopefully it's quick. Here's my code:

    PHP Code:
    class dbproject {
        var 
    $dbsys;

        function 
    dbproject($dbsys) {
            
    $this->$dbsys=$dbsys;
        }

    }

    $dbp=new dbproject('mysql');
    echo 
    $dbp->dbsys
    How would I echo the $dbsys var, which is an object in the dbproject class. I'm familiar with it in javascript, it would be dbp.dbsys, but the last line (echo $dbp->dbsys;) doesn't work. Am I constructing the class wrong? I've just got into PHP OOP, so this stuff is rather new.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yes, the constructor is incorrect:
    PHP Code:
        function dbproject($dbsys) {
            
    $this->$dbsys=$dbsys;
        }
    // Should be
        
    function dbproject($dbsys)
        {
            
    $this->dbsys $dbsys;
        } 
    Its the $this->$dbsys thats incorrect, you just need $this->dbsys. If you have it available, I'd recommend using PHP5 style OOP, which more accurately reflects an OO style programming language:
    PHP Code:
    class dbproject
    {
        private 
    $dbsys;

        public function 
    __construct($dbsys)
        {
            
    $this->dbsys $dbsys;
        }
        public function 
    getDBSys()
        {
            return 
    $this->dbsys;
        }

    }

    $dbp = new dbproject('mysql');
    echo 
    $dbp->getDBSys(); 
    The scopes give you far greater control over the properties, what can and cannot be retrieved, and how values are set. PHP5 style also lets you create object patterns, in this case a singleton factory pattern would work quite well which would force only the creation of one dbproject at a time. Patterns are quite a complex methodology since its so abstract, but a quick search on the web for a PHP singleton would give you an idea what its about!

    Hope that helps!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Thanks for the help! As for php5, I'm interested in making this app that I'm developing compatible with php4.x, but I'll be using php5 on my personal site.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No problem, I understand the backwards compatibility. I'm waiting until 6 is released before I finally retire my PHP4 code, just like I waited until 5 to retire my PHP3 code :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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