我是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 :(得分: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();
在Images类中,您将名称和源存储在两个类变量中,您可以在创建新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设置它们,或者在类变量是公共的时直接设置它们。
设置完这些后,您可以使用该类中的所有函数。例如,您可以编写一个复制图像的函数,一个调整大小或重命名的函数,一个删除它的函数。每当您需要使用具体图像时,您只需创建一个类实例并调用所需的函数
如果要执行更多操作(例如克隆图像),而不是删除原始图像,或者调整图像大小然后克隆它,则不必为图像再次设置所有设置,因为它们存储在类中并且函数可以访问它。