in Search
     
Latest post 10-14-2007 11:42 PM by lamia. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 10-13-2007 3:06 AM

    • lamia
    • Top 10 Contributor
    • Joined on 06-20-2006
    • Sampaloc, Manila
    • Posts 752
    • Points 13,185

    Need help with instance variables with PHP

    Hi guys! Since I'm not a member of any PHP forum and I'm just trying to play with PHP OOP (for some extra cash and some fun hehehe)

    I have a class which implements an interface...

     

    <?php
    //require_once("dao_includes.php");

    class MySQLDAO implements DAO
    {
        private $username;
        private $password;
        private $host;
        private $port;

        private $dsn;

        private static $instance;


        private function __construct($_username, $_password, $_host, $_port)
        {
            $username = $_username;
            $password = $_password;
            $host = $_host;
            $port = $_port;

            $dsn = $host . ":" . $port;
     
            //echo "Constructing MySQLDAO...<br/>";
        }


        /**
         *
         * @override
         */
        public function initializeConnection()
        {
            echo "username: " . $username . "<br/>";
            echo "dsn: " . $dsn;
            mysql_connect($dsn, $username, $password) or die("Cannot connect to MySQL Database");
        }


        /**
         *
         * Returns only a single instance
         */
        public static function getInstance()
        {
            if ( !isset($instance) )
            {
                //require_once("mysql_connection_properties.php");

                //$instance = new MySQLDAO($MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_HOST,

    $MYSQL_PORT);
               
                $instance = new MySQLDAO("root", "admin456", localhost, "3306");

                //return self::$instance;
                return $instance;
            }
        }


    }


    ?>

     

     

    Now, this is instantiated from a factory class...

     

    <?php
    require_once("dao_essentials.php");




    class DAOFactory
    {
        public static $MYSQL_DAO = 1;

        public static function getDAO($db)
        {
            return MySQLDAO::getInstance();
        }

    }


    ?>

     

    and eventually, I make an instance of MySQLDAO like this...

    <?php require_once("db/dao_essentials.php"); ?>

    <?php

    //phpinfo();
    $dao = DAOFactory::getDAO( DAOFactory::$MYSQL_DAO );
    $dao->initializeConnection();
    ?>



    Although the call to initializeConnection() succeeds(I can get the MySQLDAO object), my member variables $username, $password, etc. are all empty when I try to print it! Why is that?


    Thanks! 

     

    Convert limitations to great expectations... You are the creative force of your life...

    • Post Points: 5
  • 10-14-2007 4:27 AM In reply to

    • lamia
    • Top 10 Contributor
    • Joined on 06-20-2006
    • Sampaloc, Manila
    • Posts 752
    • Points 13,185

    Re: Need help with instance variables with PHP

    Problem solved. I just had to do $this->username. It's odd though...

     

     

    Convert limitations to great expectations... You are the creative force of your life...

    • Post Points: 20
  • 10-14-2007 9:40 PM In reply to

    • cruizer
    • Top 10 Contributor
    • Joined on 12-14-2005
    • Singapore
    • Posts 944
    • Points 22,590

    Re: Need help with instance variables with PHP

    ha ha, one of those PHP gotchas Stick out tongue

    unsolicited advice: don't code PHP like you're coding Java. 

    http://devpinoy.org/blogs/cruizer
    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
    • Post Points: 20
  • 10-14-2007 11:42 PM In reply to

    • lamia
    • Top 10 Contributor
    • Joined on 06-20-2006
    • Sampaloc, Manila
    • Posts 752
    • Points 13,185

    Re: Need help with instance variables with PHP

    Oh no!!!! Lolz! 

     

    Convert limitations to great expectations... You are the creative force of your life...

    • Post Points: 5
Page 1 of 1 (4 items)