Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: PHP Gurus...

  1. #1
    Join Date
    Jun 2006
    Posts
    4,832
    Tokens
    0

    Latest Awards:

    Default PHP Gurus...

    Hey, I need somebody who's good at PHP to explain something to me. I can code alright PHP, like a usersystem or whatever, but the code tends to be messy/not very efficient.

    I was looking at OOP... using classes and objects... I have no idea what they are. I looked at a few tuts and explanations and they went right over my head. I really don't understand them. Another thing is functions. I know that a function contains something and you can call that function up but I'm just as confused as to why it's helpful.

    If anybody could explain I'd be immensely grateful.

    Thanks.

  2. #2
    Join Date
    Apr 2009
    Location
    United Kingdom
    Posts
    1,111
    Tokens
    100

    Latest Awards:

    Default

    Mm. I find it helpful mainly due to the fact everything can extend each other if needed but I like private variables the most. They have come in so handy when I've been doing template classes.

    For instance, if I was to do:

    $template = new template('templateName');

    then on __construct aka public function __construct I would store the templateName as a private variable yeh.

    Then I could do stuff like

    $template->setHeader('Title of Page');
    $template->setContent('Content of page');
    $template->setFooter('Footer of Page');
    $template->setNavigation('Home', 'About', 'News', 'Blog');

    And perhaps the navigation pages are db driven or w/e

    Then setHeader, setContent, setFooter, setNavigation would be functions in the template class.

    Then you would have

    private $templateName;
    private $templateHeader;
    private $templateContent ;
    private $templateFooter;
    private $templateNavigation;

    inside the template class.

    Then at the end you can do

    $template->render();

    and it would take in all the private variables, set by the page with them simple functions and load the correct template file etc. The render would do the str_replace or w/e.

    So it's this

    PHP Code:
    <?php

    include 'template.class.php';

    $template->setHeader('Title of Page');
    $template->setContent('Content of page');
    $template->setFooter('Footer of Page');
    $template->setNavigation('Home''About''News''Blog');

    ?>
    VS.
    PHP Code:
    <?php

    include 'functions.php';

    template('Title of Page''Content of Page''Footer of page', array(cba to do array);

    ?>
    or you could do seperate procedural functions to get the parts of the template out.

    There is so many more things you could then do with the template stuff or w/e. OOP is just better. Learn it slowly. I used to use it solely for organization purposes but there are many more bonus'. I am really not the right person to explain but I tried.

  3. #3
    Join Date
    Jun 2006
    Posts
    4,832
    Tokens
    0

    Latest Awards:

    Default

    That's all well and good but I don't understand hardly any of it! I don't actually understand how it works, what the classes do, etc.

    But thanks for replying.

  4. #4
    Join Date
    Apr 2009
    Location
    United Kingdom
    Posts
    1,111
    Tokens
    100

    Latest Awards:

    Default

    Right. For example.

    We have an init file.
    PHP Code:
    <?php

    include_once 'database.core.php';

    $database = new database();

    ?>
    Now, I could change that around and have this.

    PHP Code:
    <?php

    // Include configuration file.
    include_once 'config.php';

    include_once 
    'database.core.php';

    $database = new database($dbHost$dbUsername$dbPassword$dbName);
    and have my config file like this

    PHP Code:
    <?php

    $dbHost 
    'localhost';
    $dbUsername 'Username';
    $dbPassword 'Password';
    $dbName 'dbname';
    Then this part in our init.php file:

    $database = new database($dbHost, $dbUsername, $dbPassword, $dbName);

    That is basically saying

    $database = new database('localhost', 'Username', 'Password', 'dbname');

    as you probably know. So it is creating our database class and sending variables to it.

    Now, to handle those variables, I would do something along the lines of:

    PHP Code:
    <?php

    class database {
            
        function 
    __construct($dbHost$dbUsername$dbPassword$dbName){
            
            
    // Mysql connection jazz goes here using them connection details.
            
        
    }
            
    }
    __construct is basically run when you do new object.

    So in this case I do new database in my php which tells the script or w/e to check for a construct in my database class. You have done functions before so you match them yeh and you can connect using them. Doing $database = new database() blah is a nice easy way of doing it. You can then do things such as set the links for queries to use and theres alot you can do but I'm not going into detail.

  5. #5
    Join Date
    Dec 2006
    Location
    Swindon
    Posts
    3,299
    Tokens
    215
    Habbo
    dunko

    Latest Awards:

    Default

    Quote Originally Posted by BoyBetterKnow View Post
    Right. For example.

    We have an init file.
    PHP Code:
    <?php

    include_once 'database.core.php';

    $database = new database();

    ?>
    Now, I could change that around and have this.

    PHP Code:
    <?php

    // Include configuration file.
    include_once 'config.php';

    include_once 
    'database.core.php';

    $database = new database($dbHost$dbUsername$dbPassword$dbName);
    and have my config file like this

    PHP Code:
    <?php

    $dbHost 
    'localhost';
    $dbUsername 'Username';
    $dbPassword 'Password';
    $dbName 'dbname';
    Then this part in our init.php file:

    $database = new database($dbHost, $dbUsername, $dbPassword, $dbName);

    That is basically saying

    $database = new database('localhost', 'Username', 'Password', 'dbname');

    as you probably know. So it is creating our database class and sending variables to it.

    Now, to handle those variables, I would do something along the lines of:

    PHP Code:
    <?php

    class database {
            
        function 
    __construct($dbHost$dbUsername$dbPassword$dbName){
            
            
    // Mysql connection jazz goes here using them connection details.
            
        
    }
            
    }
    __construct is basically run when you do new object.

    So in this case I do new database in my php which tells the script or w/e to check for a construct in my database class. You have done functions before so you match them yeh and you can connect using them. Doing $database = new database() blah is a nice easy way of doing it. You can then do things such as set the links for queries to use and theres alot you can do but I'm not going into detail.
    If your loading the config before setting the class up, you might aswell just do this...

    Config
    PHP Code:
    <?php
    $dbInfo
    ["username"] = "hi";
    $dbInfo["password"] = "dontlooks";
    $dbInfo["server"] = "localhost";
    $dbInfo["database"] = "hi";
    ?>
    Init
    PHP Code:
    <?php
    require("Configs/Something.php");
    require(
    "Class/Something.php");

    $class = new Thing$dbInfo );
    ?>
    Class
    PHP Code:
    <?php
    class Thing
    {
        private 
    $dbInfo;
        
        public function 
    __construct$dbInfo )
        {
            
    $this->dbInfo $dbInfo;
            
    $this->connectDatabase();
        }
        
        private function 
    connectDatabase()
        {
            
    mysql_connect$this->dbInfo["username"], etc... );
        }

    ?>

  6. #6
    Join Date
    Apr 2009
    Location
    United Kingdom
    Posts
    1,111
    Tokens
    100

    Latest Awards:

    Default

    Quote Originally Posted by Blob View Post
    If your loading the config before setting the class up, you might aswell just do this...

    Config
    PHP Code:
    <?php
    $dbInfo
    ["username"] = "hi";
    $dbInfo["password"] = "dontlooks";
    $dbInfo["server"] = "localhost";
    $dbInfo["database"] = "hi";
    ?>
    Init
    PHP Code:
    <?php
    require("Configs/Something.php");
    require(
    "Class/Something.php");

    $class = new Thing$dbInfo );
    ?>
    Class
    PHP Code:
    <?php
    class Thing
    {
        private 
    $dbInfo;
        
        public function 
    __construct$dbInfo )
        {
            
    $this->dbInfo $dbInfo;
            
    $this->connectDatabase();
        }
        
        private function 
    connectDatabase()
        {
            
    mysql_connect$this->dbInfo["username"], etc... );
        }

    ?>
    Mmm. Indeed I could. But does it really matter? No..

  7. #7
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    PHP Code:
    class database
    {
        public 
    $core;
        
        public function 
    connect()
        {
            
    $dbConnect = ( bool ) mysql_connect$this->core->__dbHost$this->core->__dbUsername$this->core->__dbPassword );
            
            if( 
    $dbConnect !== TRUE )
            {
                
    $this->core->___errorMessage = ( string ) $this->core->languages->___language'DbBadConnect' );
                return 
    FALSE;
            }
            
            
    $dbSelectDb = ( bool ) mysql_select_db$this->core->__dbDatabase );
            
            if( 
    $dbSelectDb !== TRUE )
            {
                
    $this->core->___errorMessage = ( string ) $this->core->languages->___language'dbBadDatabase' );
                return 
    FALSE;
            }
            
            return 
    TRUE;
        }
    /* ...... */

    ya can do that instead if you want to.
    Hi, names James. I am a web developer.

  8. #8
    Join Date
    Apr 2009
    Location
    United Kingdom
    Posts
    1,111
    Tokens
    100

    Latest Awards:

    Default

    Quote Originally Posted by Protege View Post
    PHP Code:
    class database
    {
        public 
    $core;
        
        public function 
    connect()
        {
            
    $dbConnect = ( bool ) mysql_connect$this->core->__dbHost$this->core->__dbUsername$this->core->__dbPassword );
            
            if( 
    $dbConnect !== TRUE )
            {
                
    $this->core->___errorMessage = ( string ) $this->core->languages->___language'DbBadConnect' );
                return 
    FALSE;
            }
            
            
    $dbSelectDb = ( bool ) mysql_select_db$this->core->__dbDatabase );
            
            if( 
    $dbSelectDb !== TRUE )
            {
                
    $this->core->___errorMessage = ( string ) $this->core->languages->___language'dbBadDatabase' );
                return 
    FALSE;
            }
            
            return 
    TRUE;
        }
    /* ...... */

    ya can do that instead if you want to.
    Loool you're telling someone who has never used OOP to use that

  9. #9
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    pretty straight forward
    Hi, names James. I am a web developer.

  10. #10
    Join Date
    Dec 2006
    Location
    Swindon
    Posts
    3,299
    Tokens
    215
    Habbo
    dunko

    Latest Awards:

    Default

    Quote Originally Posted by Protege View Post
    PHP Code:
    class database
    {
        public 
    $core;
        
        public function 
    connect()
        {
            
    $dbConnect = ( bool ) mysql_connect$this->core->__dbHost$this->core->__dbUsername$this->core->__dbPassword );
            
            if( 
    $dbConnect !== TRUE )
            {
                
    $this->core->___errorMessage = ( string ) $this->core->languages->___language'DbBadConnect' );
                return 
    FALSE;
            }
            
            
    $dbSelectDb = ( bool ) mysql_select_db$this->core->__dbDatabase );
            
            if( 
    $dbSelectDb !== TRUE )
            {
                
    $this->core->___errorMessage = ( string ) $this->core->languages->___language'dbBadDatabase' );
                return 
    FALSE;
            }
            
            return 
    TRUE;
        }
    /* ...... */

    ya can do that instead if you want to.
    I use this to connect:
    PHP Code:
    class MySQL {
        public 
    $kernel$storedFetch;
        private 
    $dbInfo$dbConnection$db$debugConnection$debugInfo$storedQuery;
        
        public function 
    __construct$kernel )
        {
            
    $this->Kernel $kernel;
            
    $this->dbInfo $this->Kernel->_loadItem"MySQL""MySQL.php""Database/""Config" );
            
    $this->_bootDatabase();
        }
            
        private function 
    _bootDatabase( )
        {
            if( empty( 
    $this->dbInfo["Database"] ) !== false )
            {
                
    $this->Kernel->alert->displayAlert"warning"1003 );
                return 
    false;
            }
            
            
    $dbInfo["Username"] = ( empty( $dbInfo["Username"] ) !== false ) ? "root" $dbInfo["Username"];
            
    $dbInfo["Server"] = ( empty( $dbInfo["Server"] ) !== false ) ? "localhost" $dbInfo["Server"];
            
            
    $this->dbConnection = @mysql_connect$this->dbInfo["Server"], $this->dbInfo["Username"], $this->dbInfo["Password"] );
            
    $this->db = @mysql_select_db$this->dbInfo["Database"] );
            
            if( 
    $this->dbConnection === false || $this->db === false )
            {
                
    // Can't connect! Lets try to debug..
                
    $this->_debugConnection();
            }
        } 

Page 1 of 3 123 LastLast

Posting Permissions

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