2013-08-04 26 views
0

我是OOP的新手,因爲我試圖讓我的代碼更高效。舉例來說,如果我有一個名爲images中,我查詢數據庫,獲得圖片src類,圖像名稱,以及其他一些字符串:在許多函數中訪問一個類中的變量

$sql = Nemesis::select("profile_picture_thumb, profile_picture_large, facebook_id", "users", "id = '{$_SESSION[user_id]}'"); 
list($profile_picture_thumb, $profile_picture_large, $facebook_id) = $sql->fetch_row(); 

有沒有在那裏我可以,也許在__construct設置這些作爲一種方式一個$var其中我可以在課堂上的許多功能訪問他們?此外,除了簡潔之外,這樣做有什麼性能優勢嗎?我會假設你從本質上查詢數據庫一次,而不是在衆多的功能之下,並將其設置爲班級績效中的「全局」會增加...或者不是?

更加明確:

class Images 
{ 
    var $main_prepend = 'm_'; 
    var $thumb_prepend = 't_'; 
    var $default_ext = 'jpg'; 
    var $cropfactor; 
    private $profile_picture_thumb; 
    private $profile_picture_large; 
    private $facebook_id; 
    public function __construct() 
    { 
     $sql = Nemesis::select("profile_picture_thumb, profile_picture_large, facebook_id", "users", "id = '{$_SESSION[user_id]}'"); 
     list($profile_picture_thumb, $profile_picture_large, $facebook_id) = $sql->fetch_row(); 
     $this->profile_picture_thumb = $profile_picture_thumb; 
     $this->profile_picture_large = $profile_picture_large; 
     $this->facebook_id = $facebook_id; 
    } 
    public function profilePic($show = true, $delete = false) 
    { 
     if ($show) { 
      echo '<script type="text/javascript">$(function() { $("#profile-picture").tipsy({fade: true}); });</script>'; 
      if (is_file(ROOT . $this->profile_picture_thumb)) { 
       echo '<img src="' . reduce_double_slashes('../' . $this->profile_picture_thumb) . '" id="profile-picture" class="profile-picture" title="Your Profile Picture">'; 
      } elseif (!empty($this->facebook_id)) { 
       // if there is no profile picture set, and user has listed fb profile picture, get profile picture 
       $fb_p_thumb = "http://graph.facebook.com/{$facebook_id}/picture"; 
       $fb_p_large = "http://graph.facebook.com/{$facebook_id}/picture?type=large"; 
       echo '<img src="' . $fb_p_thumb . '" id="profile-picture" class="profile-picture" title="Facebook Profile Picture">'; 
      } else { 
       echo '<img src="images/50x50_placeholder.gif" id="profile-picture" class="profile-picture" title="Click to add profile picture">'; 
      } 
     } 
     if ($delete) { 
      if (is_file(ROOT . $this->profile_picture_thumb) || is_file(ROOT . $this->profile_picture_larg)) { 
       if (!unlink(ROOT . $this->profile_picture_thumb) && !unlink(ROOT . $this->profile_picture_larg)) { 
        $msg->add('e', "Could not delete user profile picture!"); 
       } 
      } else { 
       $msg->add('e', "Files not found in directory."); 
      } 
     } 
    } 
    public function profilePicExists($msg = true, $delete = false) 
    { 
     if ($msg) { 
      if (is_file(ROOT . $this->profile_picture_thumb)) { 
       echo '<div class="ec-messages messages-success">Profile picture exists or was added! It may be required to refresh the page to view changes.</div>'; 
      } 
     } 
     if ($delete) { 
      if (is_file(ROOT . $this->profile_picture_thumb)) { 
       echo '<input name="doDelete" type="submit" class="btn btn-warning" id="doDelete2" value="Remove Profile Picture">'; 
      } 
     } 
    } 

不工作。

+0

當然,最好在本地存儲結果,而不是每次都讀取數據庫,是的。至於面向對象的部分,這是一個很大的話題,我建議先閱讀他們:http://www.php.net/manual/en/language.oop5.php問一個特定的部分是否讓你困惑,但你如果你首先遵循全面的指導,那麼將會得到更好的理解。 – Dave

+0

我更像是一個學習榜樣的人,我覺得這本手冊對於我的學習風格 – Alex

+1

的人來說相當沒用,這是不正確的態度。手冊有很多例子。例如,下面是一個顯示您所問的內容:http://www.php.net/manual/en/language.oop5.decon。php#example-183 – Dave

回答

0
class Images { 
    private $src; 
    private $name; 

    public function __construct($src, $name) { 
     $this->src = $src; 
     $this->name = $name; 
    } 

    public function get_src() { 
     return $this->src; 
    } 

    public function get_name() { 
     return $this->name; 
    } 
} 

$instance = new Images('image.jpg', 'Cute Duck'); 
echo $instance->get_src(); 
echo '<br>'; 
echo $instance->get_name(); 

在這裏,你的圖像類,你存儲的名稱,並在兩個類變量的來源,你可以在構造函數中,當你創建一個新的圖像類設置。您可以通過兩個吸氣功能get_name()get_src()獲取值。

你也可以設置這些變量爲公衆,嗖你可以直接訪問它們:

class Images { 
    public $src; 
    public $name; 
} 

$instance = new Images(); 
$instance->src = 'image.jpg'; 
$instance->name = 'Cute Duck'; 

echo $instance->src; 
echo '<br>'; 
echo $instance->name; 

你可以存儲並運行查詢像這樣:

class Images { 
    private $query; 
    private $result; 

    public function __construct($query) { 
     $this->query = $query; 
     //run query than store it in $this->result; 
    } 

    public function get_result() { 
     return $this->result; 
    } 
} 

$instance = new Images('SELECT * FROM stuff'); 
echo $instance->get_result(); 

這樣你就可以通過SQL語句在構造函數中,在那裏你做你的工作並存儲結果。您可以通過getter或該類中的任何其他函數來訪問結果。
請注意,這不是一個持久的解決方案,在您重新加載使用服務器端類的頁面(或轉到另一個頁面)之後,它將從頭開始。但是你可以構造你的代碼,並將常用的功能放入類中,這樣你就不必複製代碼。
例如,您可以編寫一個Image類,您可以在其中存儲名稱,大小,文件擴展名,源代碼等。您可以在創建類時將它們放入構造函數中,或者通過setter或直接設置類變量上市。
設置完這些之後,可以使用該類中的所有功能。例如,您可以編寫一個複製圖像的函數,一個調整大小或重命名圖像的函數,一個刪除圖像。無論何時您需要使用具體的圖像,您只需創建一個類實例並調用所需的函數。
如果您想要執行更多操作(例如克隆圖像)而不是刪除原始圖像或調整圖像大小然後克隆圖像,則不必再爲圖像設置所有設置,因爲它們存儲在類中並且這些功能可以訪問它。

+0

因此,我可以在__construct節中運行查詢並將其分配給可以在單獨函數中訪問的變量? – Alex

+0

有趣。我想我得到的照片。 – Alex

+0

請參閱我編輯的問題 – Alex

相關問題