duangelin7513 2013-08-04 22:23
浏览 41
已采纳

在许多函数中访问类中的变量

If, for example, I have a class called images in which I query the database to get a image src, image name, and some other strings:

$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();

Is there a way where I can, maybe in __construct set these as a $var in which I can access them in numerous functions within the class? Furthermore, are there any performance benefits in doing this aside from conciseness? I would assume since your essentially querying the database once rather than under numerous function and setting it as a "global" within the class performance would increase... or no?

More explicit:

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">';
            }
        }
    }

Does not work.

  • 写回答

1条回答 默认 最新

  • dsxz84851 2013-08-04 22:44
    关注
    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();
    

    Here in your Images class you store the name and the source in two class variables, that you can set in the constructor when you create a new Image class. You can get the values by the two getter function get_name() and get_src().

    You could also set these variables to public, sou you could access them directly:

    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;
    

    You could store and run queries like this:

    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();
    

    This way you can pass the SQL statement in the constructor, where you do your job and store the result. You can access the result through the getter or in any other function that is in the class.
    Note that this is not a persistent solution, after you reload the page (or go to another) that use the class in the server side, it starts from the beginning. But you can structure your code and put common functions that you use a lot into the class, so you don't have to duplicate code.
    For example you can write an Image class where you can store the name, size, file extension, source, etc. You can give these in the constructor when you create the class or set them via setters or directly if the class variables are public.
    After you set these, you can use all of the functions that are in the class. For example you can write a function that copy the image, one that resize or rename it, one that delete it. Any time you need to work with a concrete image you just create a class instance and call the needed functions.
    If you want to perform more operations, for example clone an image, than delete the original, or resize the image then clone it, you don't have to set all the settings again for your image because they are stored in the class and the functions have access to it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度