doubi7496 2014-02-16 08:16 采纳率: 0%
浏览 37
已采纳

php&mysql:在代码之后混淆以确定新对象的属性

I'm a new to coding, and I'm having some trouble understanding a script that sets the new object's properties. Please help me. Thank you in advance.

First, in sign_up.php, I have a input element with POST method as such...

<?php include_once('classes/signup.class.php'); ?>
...
<input type="text" class="input-xlarge" id="name" name="name" 
value="<?php echo $signUp- >getPost('name'); ?>" placeholder="<?php _e('Full name'); ?>">

In the signup.class.php, I have the following code...

class SignUp
function __construct() {
if(!empty($_POST)) {
foreach ($_POST as $field => $value)
$this->settings[$field] = $value;
$this->process();}

public function getPost($var) {
return empty($this->settings[$var]) ? '' : $this->settings[$var];}

}

$signUp = new SignUp();

To my understanding, the getPost($var) will return empty until the form is submitted. Once it is submitted, __constructor() will execute the foreach loop and for the second time, $value will call to parse getPost('name'). This time around, what is $this->settings[$var]?

I am confused, and any of your help will be much appreciated.

  • 写回答

1条回答 默认 最新

  • doujun1495 2014-02-16 09:19
    关注

    Here's your code from classes/signup.class.php (with indentation):

    class SignUp {
        function __construct() {
            if (!empty($_POST)) {
                foreach ($_POST as $field => $value) {
                    $this->settings[$field] = $value;
                }
    
                $this->process();
            }
        }
    
        public function getPost($var) {
            return empty($this->settings[$var]) ? '' : $this->settings[$var];
        }
    }
    
    $signUp = new SignUp();
    

    And here's what happens step by step:

    1. The code is being evaluated from top to bottom.

    2. The first thing that the interpreter evaluates is the definition of your class. So, after encountering class SignUp { ... }, the PHP interpreter will be aware that there is a class called SignUp but it will not execute any part from it yet.

    3. Then it encounters the $signUp = new SignUp(); instruction. At this point it instantiates an object of SignUp class and assigns it to the $signUp variable.

    4. __construct() is now being evaluated:

      4.1 As $_POST is not empty it will execute the foreach loop.

      4.2 foreach simply iterates over each value from $_POST assigning it to $this->settings.

      Example: assuming that $_POST = array('name' => 'John'), then upon the first iteration (and the only one), $field will become 'name' and $value will become 'John'.

      4.3 Now the interpreter evaluates the $this->settings[$field] = $value; expression which simply translates to $this->settings['name'] = 'John';

      4.4 The foreach loop ends and the $this->process() instruction is now being evaluated.

    5. At the end of the execution of classes/signup.class.php in your global namespace there will be an object called $signUp which contains an array member called $settings that consists only of a key ('name') being set to 'John'

    6. Now the HTML part of your file is being interpreted and when the interpreter will reach:

      value="<?php echo $signUp->getPost('name'); ?>"

      it will simply evaluate the code inside the PHP tags: echo $signUp->getPost('name');, thus calling the getPost() method on $signUp.

    7. The getPost() method simply checkes the $this->settings member (which was previously set - when __constructing the object) and returns the corresponding value for key name (which is 'John').

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

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂