Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php类名::staticFunction()->;publicFunction()这是做什么的?_Php - Fatal编程技术网

php类名::staticFunction()->;publicFunction()这是做什么的?

php类名::staticFunction()->;publicFunction()这是做什么的?,php,Php,我正在阅读php设计模式,我看到了以下代码: <?php require_once("DB.php"); class DatabaseConnection { public static function get() { static $db = null; if ( $db == null ) $db = new DatabaseConnection(); return $db; } private $_handle = null;

我正在阅读php设计模式,我看到了以下代码:

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

class DatabaseConnection
{
  public static function get()
  {
    static $db = null;
    if ( $db == null )
      $db = new DatabaseConnection();
    return $db;
  }

  private $_handle = null;

  private function __construct()
  {
    $dsn = 'mysql://root:password@localhost/photos';
    $this->_handle =& DB::Connect( $dsn, array() );
  }

  public function handle()
  {
    return $this->_handle;
  }
}

print( "Handle = ".DatabaseConnection::get()->handle()."\n" );
print( "Handle = ".DatabaseConnection::get()->handle()."\n" );
?>
但我不能这样说:

DatabaseConnection::get()->handle()->get();

除了调用get函数然后调用handle函数之外,我不明白这是在做什么

数据库连接::get()
创建
数据库连接的实例并返回它

所以

DatabaseConnection::get()->handle();
…也可以写如下

$db = DatabaseConnection::get();
$db->handle();

DatabaseConnection::get()
创建
DatabaseConnection
的实例并返回它

所以

DatabaseConnection::get()->handle();
…也可以写如下

$db = DatabaseConnection::get();
$db->handle();

这是因为静态函数返回一个新对象。这种类型的构造通常被称为单例,因为它试图强制执行只有一个
数据库连接的实例可用


请注意,构造函数是私有的,因此除非您已经在类中,否则无法显式调用
newdatabaseconnection()
。使用单例的解决方案将有一个初始为null的属性,然后在对象实例化时将该属性设置为非null值。“getInstance”(在本例中为
get
)方法仅在属性为null时返回新对象。

这是有效的,因为静态函数返回新对象。这种类型的构造通常被称为单例,因为它试图强制执行只有一个
数据库连接的实例可用


请注意,构造函数是私有的,因此除非您已经在类中,否则无法显式调用
newdatabaseconnection()
。使用单例的解决方案将有一个初始为null的属性,然后在对象实例化时将该属性设置为非null值。“getInstance”(在本例中为
get
)方法只会在属性为null时返回新对象。

我了解这一点。我主要是想知道它的get()->handler()部分。它在手册中有没有一个名字或位置,我可以在上面读到。不是真的,只是看看被退回的东西。在您的两个示例中,第一个是对DBConnection对象调用
->handle()
方法。在第二个示例中,您试图对一个简单字符串调用
->get()
方法。仅供参考,我对PHP允许第一个示例中的语法感到有点惊讶。您在第一句话中回答了我的问题,很抱歉我没有理解。我理解了很多。我主要是想知道它的get()->handler()部分。它在手册中有没有一个名字或位置,我可以在上面读到。不是真的,只是看看被退回的东西。在您的两个示例中,第一个是对DBConnection对象调用
->handle()
方法。在第二个示例中,您试图对一个简单字符串调用
->get()
方法。仅供参考,我对PHP允许第一个示例中的语法感到有点惊讶。您在第一句话中回答了我的问题,很抱歉我没有理解。我现在明白了。get()返回一个具有名为handle的函数的对象。这比我最初的想法更有意义。我现在明白了。get()返回一个具有名为handle的函数的对象。这比我最初的想法更有意义,我现在明白了。get()函数返回一个对象,该对象有一个名为handle的函数和一个名为get()的函数。这就是为什么我能够做Database::get()->get()->get()->get()->handle()。老兄,我觉得自己很笨。我错过了我的点符号。我现在看到了。get()函数返回一个对象,该对象有一个名为handle的函数和一个名为get()的函数。这就是为什么我能够做Database::get()->get()->get()->get()->handle()。老兄,我觉得自己很笨。我想念我的点符号。