Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从另一个方法内部访问一个方法_Php_Oop_Class_Methods - Fatal编程技术网

Php 从另一个方法内部访问一个方法

Php 从另一个方法内部访问一个方法,php,oop,class,methods,Php,Oop,Class,Methods,我试图从算法内部访问GCD,但它不允许我访问,我也不知道为什么。我做错了什么 public function gcd($x,$y) { do { $rest=$x%$y; $x=$y; $y=$rest; } while($rest!==0); return $x; } public function algorithm() { $a

我试图从算法内部访问GCD,但它不允许我访问,我也不知道为什么。我做错了什么

    public function gcd($x,$y) 
    {
        do {
            $rest=$x%$y;
        $x=$y;
        $y=$rest;
        } while($rest!==0);
        return $x;
    }

    public function algorithm()
    {
        $alpha = array(
            'c' => str_split('bcdfghjklmnpqrstvwxz'),
            'v' => str_split('aeiouy')
        );
        $i=$k=0;
        foreach ($this->output as $item) {
            $cnt = 0;
            $this->digits[$i] = array();
            foreach ($item as $part) {
                $this->digits[$i][$cnt] = array();
                $new = array();
                foreach ($part as $str) { 
                    $v = count(array_intersect(str_split($str), $alpha['v']));
                    $c = count(array_intersect(str_split($str), $alpha['c']));
                    $t = strlen(str_replace(' ', '', $str));

                    $new = ($cnt == 0) 
                        ? array('v' => $v, 'c' => $c, 't' => $t, 'm' => ($t%2) ? $v * 1.5 : $c) 
                        : array('v' => $v, 'c' => $c, 't' => $t);

                    $this->digits[$i][$cnt][] = $new;
                }
                $cnt++;
            }
            $i++;
        }
        $h=$a=0;
        foreach($this->digits as &$etc) {
            foreach($etc[0] as &$r){

                foreach($etc[1] as $k) {
                foreach($k as $x=>$y) {
                    $tmp[$h] = (gcd($y,$r['t']) != 1) ? ++$a:'';
                }
                    $tmp[$h] = $r['m']*$a*1.5;
                    $h++;
                    $a=0;
                }$h=0;

            $r['f'] = $tmp;
            $tmp='';
            }

        } 
    foreach($this->digits as &$u){unset($u[1]);}
    } 


但实际上,由于
gcd
不使用任何成员变量,它应该是一个自由函数。

从您使用的
public
标识符判断,我猜您的两个函数在一个类中

要引用同一对象上的方法,请使用
$this->methodname()

从您的代码:

$tmp[$h] = (gcd($y,$r['t']) != 1) ? ++$a:'';
应该是:

$tmp[$h] = ($this->gcd($y,$r['t']) != 1) ? ++$a:'';

您缺少$this->gcd内部算法 试着直接访问它
:)

为了更快更好地回答问题,最好尽可能多地删除不相关的代码,并明确指出哪一行导致错误。(最重要的是,这表明你自己做了一些努力)你说的“自由功能”是什么意思?介意解释一下吗?最简单的函数-不是成员函数,不是静态函数,不是匿名函数或lambda函数,只是。。。函数。
$tmp[$h] = ($this->gcd($y,$r['t']) != 1) ? ++$a:'';