dougan5772 2017-11-06 16:48
浏览 71

PHP全局变量范围使用最佳实践

I am new to programming and have some suspicions regarding the usage of global variables inside functions.

I am trying to separate the logic from the structure of my html form and here is where my question stems from.

The variable I have declared as global will only be used in one specific place from outside of the function, ie the html form.

function updatePosts(){
     $query= "...";
     $result= "...";
     $rows= mysqli_fetch_assoc($result);
     global $post_title;
     $post_title = $rows['post_title'] ;
}

updatePosts();

<form action="">
//use of global variable outside function
    <input type="text" name="" value="{$post_title}" class="form-control">
</form>

When i declared multiple global variables it raised some questions regarding what the impact of so many global variables might be. eg.

function updatePosts(){
     $query= "...";
     $result= "...";
     $rows= mysqli_fetch_assoc($result);
     global $post_title;
     global $post_author;
     global $post_content;
     global $post_tags;
     global $post_image;
     global $post_status;
     global $post_date;
     $post_title = $rows['post_title'] ;
     $post_author = $rows['post_author'] ;
     $post_content = $rows['post_content'] ;
     $post_tags = $rows['post_tags'] ;
     $post_image = $rows['post_image'] ;
     $post_status = $rows['status'] ;
     //and so on..
}

updatePosts();

<form action="">
<input type="text" name="" value="<?php echo $post_title ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_author ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_tags ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_content ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_status ?>" class="form-control">
//and so on...
</form>

Is this considered to be an acceptable usage of functions and global variables?

If not, what might be a more efficient way to separate the logic from the structure. Rather than encapsulating in a function would includes would be better suited for this task?

Any advice would be very much appreciated and would go a long way in helping a beginner on his journey in programming / php.

  • 写回答

1条回答 默认 最新

  • dongqiulei1987 2017-11-06 17:08
    关注

    I personally would use Closures to grab variables in your scope for further use and yes the keyword global is bad practice and in the older version it was more useful in procedual coding but today you just build tiny Closures for quick use.

    Just to give you a general idea.

    $variable_from_outside = "hi!";
    $closure = function() use ($variable_from_outside) {
        // do stuff with that variable inside this scope..
    };
    

    Closure Documentation

    However, you may also just grab your variables inside your function as parameter

    $variable = 'something';
    function func($variable) {
        // do stuff with $variable
    }
    

    And in OOP, best way are Traits. Declare universal properties or methods and use them in classes whatever your wish is. Also it reduces performance when you take traits in a later derived class for example that way they don't have to be carried in your entire API all the way.

    trait Example {
        public $stuff = 'Yey!';
        // or methods...
    }
    
    class someClass {
        use Example;
        function __construct() {
            echo $this->stuff; // Yey!
            // etc...
        }
    }
    

    Trait Documentation

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?