【问题标题】:How to define and declare a variable by calling a function in a php class如何通过调用php类中的函数来定义和声明变量
【发布时间】:2016-03-03 16:07:51
【问题描述】:

我习惯在这样的 php 函数中设置和声明我的参数:

函数 foo( $length = 24 ) { ...

现在我想用一个类函数来做到这一点:

class foo{   
    function configuration() {
      $this->defaultLength = 24;
    }
    function foo() {
      $this->configuration();
    }
    function test($length = $this->defaultLength) {...

通过调用:

$r = new foo();
$r->test();

我收到以下错误消息: 解析错误:语法错误,/www/htdo 中的意外“$this”(T_VARIABLE)..

如何在 php 类中定义和声明变量?

【问题讨论】:

    标签: php


    【解决方案1】:

    我会这样做:

    class foo
    {   
        const DEFAULT_LENGHT = 24;
    
        public function test($length = self::DEFAULT_LENGHT)
        {
            var_dump($length);    
        }
    }
    
    $foo = new foo();
    $foo->test(); // // int(24)
    

    如果您需要在运行时更改该值,您可以执行以下操作:

    class foo
    {   
        protected $default_lenght = 24;
    
        public function setDefaultLenght($lenght)
        {
           $this->default_lenght = $lenght;
        }
    
        public function test($length = null)
        {
            $length = $length ?: $this->default_lenght;
            var_dump($length);
        }
    }
    
    $foo = new foo();
    $foo->test(); // int(24)
    $foo->setDefaultLenght(42);
    $foo->test(); // int(42)
    

    【讨论】:

    • @E_p,我的错,对不起,我分心了
    • NP 在测试功能和代码审查通过之前添加 public :)。
    • 这真的不是我的日子,我发誓我始终遵循 PSR-2 规范:P
    【解决方案2】:

    您不能在函数签名中引用$this。你可以这样做:

    public function test ($length = null) {
        if ($length === null) {
            $length = $this->defaultLength;
        }
    }
    

    【讨论】:

    • 他为什么不能?如果他想将它用作$r->test();,那么$this->defaultLength 是可访问的,如果他在同一个作用域(类内部)中声明它。
    • 函数内部,是的。但不在函数签名内部,因为函数签名是在创建任何实例之前读取的。
    【解决方案3】:

    您可以使用函数签名的元素构建一个数组,过滤它们并将其与调用您需要的所有函数的默认配置合并。 我做了一个简单的例子,定义了两个不同功能的参数。

    class foo{   
    
        private $configuration;
    
        private function buildDefaultConfiguration() {
            return $configuration = array(
                'defaultLength' => $this->getDefaultLength(),
                'defaultWidth'  => $this->getDefaultWidth(),
            );
        }
    
        public function __construct($givenLength = null, $givenWidth = null) {
            $givenConfiguration = array(
                'defaultLength' => $givenLength,
                'defaultWidth'  => $givenWidth
            );
            $givenConfiguration = array_filter($givenConfiguration);
            $defaultConfiguration = $this->buildDefaultConfiguration();
            $this->configuration = array_merge($defaultConfiguration, $givenConfiguration);
        }
    
        public function test()
        {
            echo $this->configuration['defaultLength'] . ' - ' . $this->configuration['defaultWidth'];
        }
    
        private function getDefaultLength()
        {
            return 24;
        }
    
        private function getDefaultWidth()
        {
            return 12;
        }
    }
    
    $foo1 = new foo(13,14);
    $foo2 = new foo();
    $foo3 = new foo(null, 66);
    $foo4 = new foo(66);
    
    $foo1->test();
    //result is 13 - 14
    $foo2->test();
    //result is 24 - 12
    $foo3->test();
    //result is 24 - 66
    $foo4->test();
    //result is 66 - 12
    

    您可以查看实时工作示例here,但格式不是很好 希望对你有帮助

    【讨论】:

      【解决方案4】:
      class foo{  
      //set the default lenght first 
      var $defaultLength = 0;
      
      function configuration() {
        $this->defaultLength = 24;
      }
      
      
      function setLength($newLength){
       $this->defaultLength = $newLength;
      }
      
      function test($length = null) {
       //if length is null assign default
       if($length == null){
      $length = $this->defaultLength; 
       }
      
      }
      
      $foo = new foo();
      $this->configuration();
      $foo->test(); // int(24)
      $foo->setLength(42);
      $foo->test(); // int(42)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-06
        • 2022-12-31
        • 2011-04-30
        • 2018-11-16
        • 2011-07-24
        • 2021-01-02
        • 1970-01-01
        • 2018-02-04
        相关资源
        最近更新 更多