PHP OOP最佳实践或如何编码正确?

I'm trying to learn how to properly code PHP OOP. This is where I'm running into issues.

I created several classes that extend main Application class and I want to make things work properly.

I have main file that's index.php that looks like this:

include_once('classes/Application.php');
include_once('classes/Configuration.php');
include_once('classes/Database.php');

$app = new Application;
$config = new Configuration;
$db = new Database;

var_dump($app->db_connected);
var_dump($db->db_connected);

$db->connect($config->dbhost, $config->dbuser, $config->dbpass, $config->dbname);

var_dump($app->db_connected);
var_dump($db->db_connected);

The output is:

1. bool(false)
2. bool(false)
3. bool(false)
4. bool(true)

My main application file looks like this:

class Application {
    public $db_connected = false;
}

And my Database class looks like this:

class Database extends Application {
    function connect($dbhost, $dbuser, $dbpass, $dbname) {
        if(!$this->db_connected) {
            mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
            mysql_select_db($dbname) or die(mysql_error());
            $this->db_connected = true;
        }
    }
}

So the question is, why would line #3 of the output of index.php display false? The db_connected property has been overridden in Database class and set to TRUE, but it still returns false.

Although when accessed directly from Database class instance it shows TRUE correctly. What's the deal here?

Also when does the class EXTEND command occurs? Whenever parent class' instance is created or I have to manually create instance of the child class?

It seems you are reaching for the concept of of a static variable all instances of a class share the same static variable so using the new twice will not be an issue.

You can see the code on ideaone.

// your code goes here

class Application {
    static $db_connected = false;
}

class Database extends Application {
    function connect() {
        if(!static::$db_connected) {
            static::$db_connected = true;
        }
    }
}

$app = new Application;
$db = new Database;

var_dump(Application::$db_connected);
var_dump(Database::$db_connected);

$db->connect();

var_dump(Application::$db_connected);
var_dump(Database::$db_connected); 

Your comment make me think you are looking for a better pattern all together. I would like to throw out some key principles namely OCP and LSP SOLID.

In this case you would avoid having Application being an instance of Database but instead use dependency injection. Here is the refactored code.

class Database {
   private $db_connect = false;
   public function connect () {
      if(!$this->db_connect) { /* do connection */ }
   }
}
class Application {
   private $db;
   public function setDatabse(Database $db) {
      $this->db = $db;
   }

   public function getDatabase() {
      return $this->db;
   }
}

$db = new Database;
$app = new Application;
$app->setDatabase($db);
$app->getDatabase()->connect();

This line is your hint

Although when accessed directly from Database class instance it shows TRUE correctly. What's the deal here?

You have 2 instances. Above you are checking $db instance which you connected with, and then you print from $app which was never connected. They are separate entities, one is connected one is not.

Extend occurs as soon as the file is loaded, read by the php interpreter, this happens regardless of ever using the class.

Extend is called from the child and inherits everything form the class it extends. So if you call a child method in the parent, well you are doing it backwards. It goes one way, Prent -> Child.

I would use Dependance injection for the database, then you can reuse it's code.

Like this:

 //parent class
class Application {
    //holds a reference to the Database class
    protected static $db_conn = false;

    public function __construct($db){
        self::$db_conn = $db;
    }
}

//child class of Application
class Application2 extends Application {
    public function getSomething($id){
        return self::$db_conn->getbyId($id) ;
    }
}

 //separate utility class
class Database{
    static $conn;

    public function __construct( $dbhost, $dbname, $dbuser, $dbpass, $dbname) {
        static::$conn = mysqli_connect($dbhost, $dbuser,$dbpass,$dbname);
    }

    public function getbyId( $id ){
        ..code to get stuff by id using $conn - previous connection ...
        return $result;
    }
}

$db = new Database("myhost", "myuser", "mypassw", "mybd");
$app = new Application2( $db );
$app->getSomething(1);
//create another app with the same database connection, this is the value of injecting it. 
$second_app = new Application2( $db );

See you can reuse database over and over, you can replace it without changing the code in Application as long as the calls for the functions of the Database class don't change. Each thing is responsible for it's own business.

This is called separation of concerns.

Inheritance is good, when it's needed. You might have an basic application for free users of you're services and then extend that with a premium application for paid members. Sense they paid they get all the free functionality, but also the premium stuff to.

In my example above the database is something they both need, as well as other things will probably use this. Such as a login system may need a database connection, payment system might, a shopping cart might. These are all separate objects, they don't / nor should they extend off of one Master Class, that's a bad idea. Keep them separate.

STATIC

I seen mention of the :: static object operator. My example is a bit flawed when using the static property protected static $db_conn = false;

  $app = new Application2( $db );
  $second_app = new Application2( $db ); //assigning db 2x is not needed.

The reason for :: and the -> normal way. Is that static :: is shared across all instance of a class, and -> is just this instance of the class. I had assigned the $db class to a static variable 2 times a better way would have been like this.

  //parent class
class Application {
    protected static $db_conn = false;
    //separate method then construct.
    public function connect($db){
        self::$db_conn = $db;
    }
}

//we'll keep the rest of the code the same here.

$db = new Database();
$app = new Application2();
$app->connect( $db );
$second_app = new Application2();
$second_app->getSomething(1);

Now in this example $second_app never ran it's connect method. But because the first $app did and because the static for the database variable protected static $db_conn. Now all classes that have extended the Application class have a database connection. This is what static does. It's value is shared across all instance of the class. So when you see :: think all class instance and when you see -> think only this class instance. It's actually one thing I love about php, makes it so much easier to keep track of then in some other languages.

Not to confuse you but the other use of the :: is not actually needing an instance at all. Assume you have a Config class like this.

  class Config{
         static $db = 'hello';
         static $items = array('one' => 'item 1' );

         private __construct(){} // no construction allowed

         static function getItem( $which ){
             return self::$items[$which];
        }
  }

Now without ever creating an instance of the class by calling new Config() , you can simply.

 echo Config::$db;
   // prints hello
 echo Config::getItem('one');
   // prints 'item 1'

This is quite use full for config type classes. Where they are an empty shell just used to store data in and you don't need an object for them, essentially a way to keep things organized. So tying this in to the previous examples

$db = new Database(Config::$myhost, Config::$myuser, Config::$mypassw, Config::$mybd);

In your case best OOP practice is to use Mediator pattern. Concrete Mediator will be Application class:

class ApplicationBase {
    private $db;
    private $cfg;

    public function setDb(Database $db) {
        $this->db = $db; return $this;
    }
    public function setConfig(Config $cfg) {
        $this->cfg = $cfg; return $this;
    }
}

class Application extends ApplicationBase {
    public function getDsn() {
        return $this->cfg->getDsn();
    }
    public function getDbUser() {
        return $this->cfg->getDbUser();
    }
    public function getDbPass() {
        return $this->cfg->getDbPass();
    }
    public function getConnection() {
        return $this->db->getConnection();
    }
}

class AppComponent {
    protected $app;

    public function __construct(Application $app) {
        $this->app = $app;
    }
}

class Config extends AppComponent {
    private $dsn;
    private $dbuser;
    private $dbpass;
    // ... getters and setters
}

class Database extends AppComponent {
    private $connection;

    private function connect() {
        $this->connection = new PDO(
            $this->app->getDsn(),
            $this->app->getUser(),
            $this->app->getPass()
        );
    }

    public function getConnection() {
        if (null === $this->connection) $this->connect();

        return $this->connection;
    }
}

class Model extends AppComponent {
    protected $table;
    // Model stuff here
}

class Content extends Model {
    public function getNews() {
        $db = $this->app->getConnection();
        return $db->query("SELECT * FROM $this->table LIMIT 5")->fetchAll();
    }
}

Such architecture will be enough for simple, clean-looking applications and classes will be ready for easy unit-testing:

$app = new Application();
$cfg = new Config($app);
$db  = new Database($app);
$app->setDb($db)->setConfig($cfg);

$content = new Content($app);
$news    = $content->getNews();