why are the class.Database.inc file changes working?

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28226938.html

Because this is a lynda.com tutorial, I want to understand code.


I am not understanding why Ray's class.Database.inc files work instead of the original class.Database.inc file
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Here is the original code.  I can show you why this failed.  I do not think I did much but correct a typographical error.  It was difficult to find the error, but in the end there was a very simple solution.

On line 6: private $_connection;
On line 41: return $this->_connection;

In both of these statements the variable name is $_connection, with a leading underscore.

Now look at line 24: $this->connection = new mysqli

The leading underscore is missing on line 24.  $this->connection and $this->_connection are not the same variable.  The script assigned a MySQLi object to $this->connection, but when the getConnection() method was called, it returned $this->_connection, which contained NULL.

Does that help to answer it?

<?php
/**
* mysqli database: only one connection is allowed
*/
class Database{
  private $_connection;
  //store the single instance
  private static $_instance;
  /**
  * get an instance of the database
  * @return Database
  * 
  */
  public static function getInstance(){
    if (!self::$_instance){
      self::$_instance= new self();
    }
    return self::$_instance;
  }
  /**
  * constructor
  */
  public function __construct(){
    $this->connection = new mysqli('localhost','username','pass','dbname');
    //error handling
    if (mysqli_connect_error()){
      trigger_error('Failed to connect to MySql: '.mysqli_connect_error(),E_USER_ERROR);
    }

  }
  
  
    /**
    * Empty clone magic method to prevent duplication.
    */
    private function __clone(){}
    /**
    * get the mysqli connection
    */
    public function getConnection(){
      return $this->_connection;
    }  
}

Open in new window

The practice of naming a variable starting with an underscore is one that I question.  Not only does it make the names a bit fiddly and harder to read, especially in OOP notation which is full of -> and the like, but it gets close to confusion with the leading double underscore which is a notation that is reserved by PHP for its own use.  I understand why we have done this in the past, but on balance it seems to be one of those things like global -- well intentioned but ultimately troublesome with unexpected consequences.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
In both of these statements the variable name is $_connection, with a leading underscore.

Now look at line 24: $this->connection = new mysqli


thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.