2011-05-16 66 views
5

Tôi có hai giá trị tĩnh: "type" và "typeID". Loại là con người có thể đọc được và không đổi, và typeID cần phải được tra cứu từ cơ sở dữ liệu, dựa trên giá trị của kiểu. Tôi cần tra cứu để xảy ra một lần, khi định nghĩa lớp được nạp lần đầu tiênTự động điền biến tĩnh trong PHP

Để minh họa, đây là một số mã không hoạt động vì bạn không thể gọi hàm trong vùng khai báo.

MyClass extends BaseClass { 
    protected static $type = "communities"; 
    protected static $typeID = MyClass::lookupTypeID(self::$type); 
} 

Có phương pháp kỳ diệu nào được gọi chính xác khi định nghĩa lớp được tải không? Nếu có một cái gì đó hiển nhiên tôi đang thiếu nó.

+0

thể trùng lặp của [PHP: Làm thế nào để khởi tạo các biến tĩnh] (http://stackoverflow.com/questions/693691/php-how-to-initialize-static-variables) – webbiedave

+0

@webbiedave - đó là về init ialization và nó xảy ra là nguyên nhân gốc rễ, nhưng tôi cho rằng câu hỏi là khác nhau. – slifty

+0

Populating, khởi tạo. Cà chua, cà chua :) Tôi nghĩ nó giống nhau nhưng tôi nghiêm túc nghi ngờ nó sẽ đóng cửa vào thời điểm này. – webbiedave

Trả lời

9

trơ trẽn kéo từ ý kiến ​​từ khóa tĩnh hướng dẫn php của:

Because php does not have a static constructor and you may want to initialize static class vars, there is one easy way, just call your own function directly after the class definition. 

for example. 

<?php 
function Demonstration() 
{ 
    return 'This is the result of demonstration()'; 
} 

class MyStaticClass 
{ 
    //public static $MyStaticVar = Demonstration(); //!!! FAILS: syntax error 
    public static $MyStaticVar = null; 

    public static function MyStaticInit() 
    { 
     //this is the static constructor 
     //because in a function, everything is allowed, including initializing using other functions 

     self::$MyStaticVar = Demonstration(); 
    } 
} MyStaticClass::MyStaticInit(); //Call the static constructor 

echo MyStaticClass::$MyStaticVar; 
//This is the result of demonstration() 
?> 
1

như vậy là một điều thường được gọi là "constructor tĩnh", nhưng PHP thiếu những việc như vậy. Bạn có thể muốn xem xét một trong các giải pháp được đề xuất trong các nhận xét thủ công PHP, ví dụ: http://www.php.net/manual/en/language.oop5.static.php#95217

3

Đơn giản và không cần phép thuật, đừng quên bạn luôn có thể xác định biến là rỗng và kiểm tra biến đó là rỗng (chỉ thực hiện cuộc gọi db). Sau đó, nó chỉ là vấn đề nếu bạn muốn điều đó xảy ra khi lớp được xây dựng hoặc bao gồm (include_once vv ...)

MyClass extends BaseClass { 
    protected static $type = "communities"; 
    protected static $typeID = null; 

    public function __construct(){ 
     if(is_null(self::$typeID)){ 
      self::lookupTypeID(self::$type); 
     } 
    } 

    public static lookupTypeID($type){ 
     self::$typeID = //result of database query 
    } 
} 

hoặc

MyClass::lookupTypeID(); //call static function when class file is included (global space) 

MyClass extends BaseClass { 
    protected static $type = "communities"; 
    protected static $typeID = null; 

    public function __construct(){ 

    } 

    public static lookupTypeID($type=null){ 
     if(is_null($type)){ 
      $type = self::$type; 
     } 
     self::$typeID = //result of database query (SELECT somefield FROM sometable WHERE type=$type) etc.. 
    } 
} 

một constructor tĩnh là giống như một phương pháp nhà máy

if(!function_exists(build_myclass)){ 
    function build_myclass(){ 
     return MyClass::build(); 
    } 
} 

MyClass extends BaseClass { 
    protected static $type = "communities"; 
    protected static $typeID = null; 

    public function __construct(){ 

    } 

    public static function build(){ 
     return new self(); //goes to __construct(); 
    } 

} 

$class = new MyClass(); //or 
$class = MyClass::build(); //or 
$class = build_myclass(); 
Các vấn đề liên quan