PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

get_class_methods> <class_exists
Last updated: Fri, 30 Jan 2009

view this page in

get_called_class

(No version information available, might be only in CVS)

get_called_class"静的遅延束縛" のクラス名

説明

string get_called_class ( void )

静的メソッドのコール元のクラス名を取得します。

返り値

クラス名を返します。クラスの外部からコールされた場合は FALSE を返します。

例1 get_called_class() の使用法

<?php

class foo {
    static public function 
test() {
        
var_dump(get_called_class());
    }
}

class 
bar extends foo {
}

foo::test();
bar::test();

?>

上の例の出力は以下となります。

string(3) "foo"
string(3) "bar"

参考

  • get_parent_class() - オブジェクトの親クラスの名前を取得する
  • get_class() - オブジェクトのクラス名を返す
  • is_subclass_of() - あるオブジェクトが指定したクラスのサブクラスに属するかどうかを調べる



add a note add a note User Contributed Notes
get_called_class
danbettles at yahoo dot co dot uk
08-Oct-2008 10:00
It is possible to write a completely self-contained Singleton base class in PHP 5.3 using get_called_class.

<?php

abstract
class Singleton {

   
protected function __construct() {
    }

   
final public static function getInstance() {
        static
$aoInstance = array();

       
$calledClassName = get_called_class();

        if (! isset (
$aoInstance[$calledClassName])) {
           
$aoInstance[$calledClassName] = new $calledClassName();
        }

        return
$aoInstance[$calledClassName];
    }

   
final private function __clone() {
    }
}

class
DatabaseConnection extends Singleton {

   
protected $connection;

   
protected function __construct() {
       
// @todo Connect to the database
   
}

   
public function __destruct() {
       
// @todo Drop the connection to the database
   
}
}

$oDbConn = new DatabaseConnection();  // Fatal error

$oDbConn = DatabaseConnection::getInstance();  // Returns single instance
?>

Full write-up in Oct 2008: http://danbettles.blogspot.com

get_class_methods> <class_exists
Last updated: Fri, 30 Jan 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites