0

有人可以解释我并为一起工作的静态和非静态类做一些例子吗?

喜欢:

App::MySQL()->insert();

App::User()->name;

我在网上找不到任何东西。

谢谢

4

2 回答 2

0
<?php

class App {

    public static function MySQL() {
        return new MySQL();
    }
}

class MySQL {

    public function insert() {
        echo "inserting some stuff\n";
    }

}


App::MySQL()->insert();

ivo@atmycomputer ~$ php test.php 
inserting some stuff
ivo@atmycomputer ~$ 
于 2013-10-05T17:48:08.033 回答
0

The first part (e.g. App::MySQL(), App::User()) is calling a static method on a class in order to return a singleton instance (meaning if an instance of that class exists return it, else create a new one and return it), this makes sure that you have at most one instance of that class. Then you call its methods or get its properties (the second part: ->insert(), ->name)

于 2013-10-05T17:46:14.280 回答