dougan5772 2011-02-20 09:52
浏览 78
已采纳

像PHP一样使PHP加入/ concat(。)

I've stuck on a seems-to-be simple command join, but can't work it out.

I have a between() function which does the following:


/**
 * Checks if passed int is between $left and $right
 * @param int $left lowest value
 * @param int $right highest value
 * @param int $value actual value
 * @return bool is $value between $left and $right
 */
function between($left, $right, $value)
{
    $value = intval($value);
    return ( $value >= $left && $value <= $right );
}

and the usage is pretty simple:

$int = 9;

var_dump( between( 6, 14, $int ) );//bool(true)

Now what I want to achieve is:


$int = 9;

var_dump( $int.between( 6, 14 ) );//bool(true)

it would make more sense and would be easier to understand.

Any ideas how do I achieve this?

If $int would be an object which extends comparisonFunctions I could do $int->between(); but maybe there is a way to catch what does . join?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • douzi9744 2011-02-20 09:55
    关注

    $int is of the primitive type int and contains the value 9. It is not an object that has instance methods/functions. This (sadly) isn't Ruby ;)

    What you want isn't possible in PHP unless you do something like this - but I wouldn't advise it:

    class Integer {
    
        private $value;
    
        public function  __construct($value) {
            $this->setValue((int)$value);
        }
    
        public function getValue() {
            return $this->value;
        }
    
        public function setValue($value) {
            $this->value = $value;
        }
    
        public function between($a, $b) {
            return ($this->getValue() >= $a && $this->getValue() <= $b);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?