例如,如果我有一个名为的类,我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">';
}
}
}
不工作。