【问题标题】:What happens in this code block during php oop?在 php oop 期间这个代码块会发生什么?
【发布时间】:2011-12-09 04:25:04
【问题描述】:

有人可以解释一下第三行使用 Request 和 $request 的地方吗?如果您能给我提供一个具有相同解释的链接,那就太好了?我只是想知道那里发生了什么。

    <?php
       class xyz {
        public function foo(Request $request){
          //some code
        }
       }

【问题讨论】:

  • 你从哪里得到代码?

标签: php oop type-hinting


【解决方案1】:

类型提示:

http://php.net/manual/en/language.oop5.typehinting.php

<?php
// An example class
class MyClass
{
    /**
     * A test function
     *
     * First parameter must be an object of type OtherClass
     */
    public function test(OtherClass $otherclass) {
        echo $otherclass->var;
    }


    /**
     * Another test function
     *
     * First parameter must be an array
     */
    public function test_array(array $input_array) {
        print_r($input_array);
    }
}

// Another example class
class OtherClass {
    public $var = 'Hello World';
}

如果参数不是指定的类型,则会抛出错误:

<?php
// An instance of each class
$myclass = new MyClass;
$otherclass = new OtherClass;

// Fatal Error: Argument 1 must be an object of class OtherClass
$myclass->test('hello');

// Fatal Error: Argument 1 must be an instance of OtherClass
$foo = new stdClass;
$myclass->test($foo);

// Fatal Error: Argument 1 must not be null
$myclass->test(null);

// Works: Prints Hello World
$myclass->test($otherclass);

// Fatal Error: Argument 1 must be an array
$myclass->test_array('a string');

// Works: Prints the array
$myclass->test_array(array('a', 'b', 'c'));
?>

【讨论】:

    【解决方案2】:
    【解决方案3】:

    Request 类型的对象正在传递给函数 foo。

    它在名为 $request 的私有变量中可供函数 foo 使用。

    【讨论】:

      【解决方案4】:

      这是一个type hint,用来告诉 php 期待一个具有的对象

      $request instanceof Request == true
      

      请注意,这实际上不会确保任何事情。如果 $request 为 null 或其他对象,则很可能只会抛出 可捕获的致命错误,因此无论如何您都必须测试有效值。

      【讨论】:

        【解决方案5】:

        第三行定义了一个名为 foo 的类方法,它可以获取“Request”类型的 $request 参数。

        这是针对类开发人员的安全措施。确定

        <?php
            class User
            {
                private $username;
        
                public function get_username()
                {
                    return $this->username;
                }
            }
        
            class xyz()
            {
                public function foo(User $currentUser)
                {
                    $currentUser->get_username();
                }
            }
        
            $x = new xyz();
        
            $u = new User();
            $x->foo($u);           // That will not produce any error because we pass an Object argument of type User
        
            $name = "my_name";
            $x->foo($name);        // This will produce an error because we pass a wrong type of argument
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多