我是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">';
}
}
}
不工作。
當然,最好在本地存儲結果,而不是每次都讀取數據庫,是的。至於面向對象的部分,這是一個很大的話題,我建議先閱讀他們:http://www.php.net/manual/en/language.oop5.php問一個特定的部分是否讓你困惑,但你如果你首先遵循全面的指導,那麼將會得到更好的理解。 – Dave
我更像是一個學習榜樣的人,我覺得這本手冊對於我的學習風格 – Alex
的人來說相當沒用,這是不正確的態度。手冊有很多例子。例如,下面是一個顯示您所問的內容:http://www.php.net/manual/en/language.oop5.decon。php#example-183 – Dave