dtrnish3637 2010-06-24 09:15
浏览 48
已采纳

在类函数中设置变量,我可以在哪里使用该变量

If I have a class (like below) and in a function I set a variable (below:$this->example) but I havent declared the variable at the top of the class (e.g var $example;) where and how can I use this variable? I tried using it straight away in another function but it didn't work (I guess I could have made a mistake but it worked after I declared it in the top of the class)

I have seen this in Symfony for setting variables that you can use in the view and also I came accross it in Phorms to name a couple of examples.

Sorry if this is obvious, I would just like to understand how I can use these variables, including getting the name of the variable(e.g $this->example, by name I mean "example").

class ExampleClass{
  var $another_var;
  function __construct($data){
    $this->example = $data;
    $this->another_var = $data;
  }

  function exampleFunction(){
    $test = $this->example; //this doesnt work
    $another_test = $this->another_var; //this obviously does
  }
}

Any help would be much appreciated

Regards

Luke

EDIT: (from my reply to DrColossus)

I want to be able to set any variable name in a function and in another function grab any variables set with there name.For example in Symfony I can set $this->completly_random_name = $x in an action class function, then in the view I can use $completly_random_name. There is no way that symfony has set every possible combination of variable names in the top of the parent class.

  • 写回答

4条回答 默认 最新

  • duanji1043 2010-06-24 09:38
    关注

    Skipping the talk of how you normally shouldn't do this, you can accomplish what you want using PHP's magic __get and __set functions.

    class MyClass {
      // add one member var that holds all the data
      private $myMagicData = array();
    
      private $another_var;
    
      function __construct($data){
        $this->example = $data; // this will auto-call the __set function
        $this->another_var = $data; // this won't, since $this->another_var was declared above
      }
    
      function __get($name) {
        return array_key_exists($name, $this->myMagicData) ? $this->myMagicData[$name] : null;
      }
    
      function __set($name, $value) {
        $this->myMagicData[$name] = $value;
      }
    
      function exampleFunction(){
        $test = $this->example; // this will auto-call the __get function
        $another_test = $this->another_var; // this won't
      }
    }
    

    Documentation on these and other magic functions.


    Why you shouldn't do this.

    In my opinion, using the magic __get and __set functions promotes poor programming practice. Let me demonstrate using a famous example: If a glass is half-filled, is the glass half-full or half-empty? The correct answer from a programmer's point of view is that the glass is too large. What I mean by this is, when you add the magic functions as demonstrated above, you can just keep on assigning variables and it won't care, but are they necessary?

    Over time, your code will change and you might no longer need old variables that were previously assigned. Normally, you would just remove the variable declaration, meaning your class will now consume less (unneeded) memory. If you forgot to remove one of the old assignments, you'll find out soon enough. With the magic function functionality, how are you going to keep track of which variables you need, and which you don't?

    Remember that code should be written primarily for humans to read, and only secondarily for machines to execute. If a second person were to join you and he wonders what variables he has access to in the view, he would either have to go through the code assigning the variables, or print_r($this->myMagicData), rather than just looking at the section of the class where the variables are declared.

    And, of course, there is also the overhead of the magic functions getting called, which may or may not be a concern depending on the situation.

    So, to summarize, manually declaring the variables you need helps:

    1. Keep track of what data you are and aren't using
    2. Makes your code easier to read for both you and others
    3. Performs faster

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程