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.

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?