ehime
ehime

Reputation: 8375

Accessing a Method from inside Another Method

I was trying to access GCD from inside Algorithm but it's not letting me and I'm not sure why. What am I doing wrong here?

    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]);}
    } 

Upvotes: 0

Views: 129

Answers (3)

Wallysson Nunes
Wallysson Nunes

Reputation: 750

You're missing $this->gcd inside algorithm just tryng to acces it direct :)

Upvotes: 0

Jonathan Fingland
Jonathan Fingland

Reputation: 57167

judging by your use of the public identifier, I would guess your two functions are in a class.

To refer to methods on the same object, use $this->methodname()

from your code:

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

should be:

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

Upvotes: 1

xtofl
xtofl

Reputation: 41509

...

$this->gcd()?

But really, since gcd doesn't use any member variables, it should be a free function.

Upvotes: 0

Related Questions