SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __construct & require_once()

    Hi,

    can you not use a require_once within the __construct() function of a class when the required script it set as a variable (i.e. require_once($this->_db_connection_vars))
    Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, require_once() should be fine with a __construct, why do you think it wouldn't be?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Sure, require_once() should be fine with a __construct, why do you think it wouldn't be?
    I think it because it works when i define the script but not when i use a variable

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Does the variable have scope within the class? Could you post a quick sample?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the class
    PHP Code:
    class __cms{

        
    // Define variables for use with cms
        
    private $_hDB// use for connection to mysql server
        
    public $_root;
        public 
    $_urlroot;
        public 
    $_mysql_connection_vars;
        
        public function 
    __construct(){
            
            require_once(
    $this->_mysql_connection_vars);
            
        }


    This is the script requesting the class

    PHP Code:
    $_cms = new __cms();
        
    $_cms->_mysql_connection_vars '../dbsettings.php'

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You are setting the path for $_mysql_connection_vars AFTER the class is instantiated, __construct runs once you specify 'new __cms' and at that point, $_mysql_connection_vars has no value.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    An alternative would be:-

    PHP Code:
    <?php
    class __cms
    {
        
    // Define variables for use with cms

        
    private $_hDB// use for connection to mysql server

        
    public $_root;

        public 
    $_urlroot;

        public 
    $_mysql_connection_vars;

        

        public function 
    __construct$dbPath )
        {
            
    $this->_mysql_connection_vars $dbPath;
            require_once(
    $this->_mysql_connection_vars);    
        }



    new 
    __cms'../dbsettings.php' );

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, i didnt think of that lol, thanks.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No worries, although , if your trying to create a OOP CMS maybe you should be passing your Database object to the CMS?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    No worries, although , if your trying to create a OOP CMS maybe you should be passing your Database object to the CMS?
    Hi,
    What do you mean?

    Thanks.

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Whilst there is a lot to go into when creating an application like this, you would ideally want to create separate objects for say ''Database','Settings','Template' and such...

    It would allow you to change a certain object without changing any of the other object, say moving from MySQL to Oracle, or from Smarty to your own templating engine.

    Here is a very crass, rough and dirty example.
    PHP Code:
    <?php
    class Database
    {
        private 
    $oHandle;
        
        public function 
    __construct$sHost$sUser$sPass$sName$iPort )
        {
            
    $this->oHandle = new mysqli$sHost$sUser$sPass$sName$iPort );
        }
        
        public function 
    query$sSQL )
        {
            return 
    $this->oHandle->query$sSQL );
        }
        
        public function 
    escape$sValue )
        {
            return 
    $this->oHandle->real_escape_string$sValue );
        }
    }

    class 
    CMS
    {
        protected 
    $oDatabase;
        
        public function 
    __construct(Database $oDatabase)
        {
            
    $this->oDatabase $oDatabase;
        }
        
        public function 
    getUserDetails$iID )
        {
            
    $oResult $this->oDatabase->query(sprintf("SELECT username,password,name,sex FROM users WHERE id = %s;",(Integer)$iID));
            
    /*
            //--> Do stuff with result and return array.
            */
        
    }
    }

    //--> Usage
    try
    {
        
    $oDatabase = new Database'myserver','me','mypassword','cms_db',3306 );
        
    $oCMS = new CMS$oDatabase );
    }
    catch( 
    Exception $oException )
    {
        die(
    'Sorry, there is no database connection at this time.');
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •