例如,如果我有一个名为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">';
}
}
}不管用。
发布于 2013-08-04 22:44:38
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();在您的Image类中,您可以将名称和源存储在两个类变量中,在创建新的Image类时可以在构造函数中设置这些变量。您可以通过两个getter函数get_name()和get_src()获得值。
您还可以将这些变量设置为public,您可以直接访问它们:
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设置这些类时,或者如果类变量是公共的,可以在构造函数中给出这些类。
设置这些之后,可以使用类中的所有函数。例如,您可以编写一个复制图像的函数,一个调整图像大小或重命名的函数,一个删除它的函数。每当您需要处理具体的映像时,只需创建一个类实例并调用所需的函数即可。
如果您想要执行更多的操作,例如克隆图像,而不是删除原始图像,或者调整图像大小然后克隆它,则不必再次设置图像的所有设置,因为它们存储在类中,并且函数可以访问它。
https://stackoverflow.com/questions/18048195
复制相似问题