Execute function of the same class [closed]

-1

I've tried several forms here on the site and on the net, but no other functions. I have a form that when sending it loads the class and function:

class editar {
public function selecao_editar() {
//Aqui tem outro form que ao enviar deve carregar a outra função para atualizar o banco.    
}

function update(){
if (isset($_POST['botaoupdate'])) {
....}
}
}

I have tried the following ways:

1°
class editar {
    public function selecao_editar(){
        $temp = editar ::update();
        ...
    }

    function update(){
        ...
    }
}

2°
class editar {
    public function selecao_editar(){
        $this-> update();
        ...
    }

    function update(){
        ...
    }
}
    
asked by anonymous 19.07.2016 / 13:37

2 answers

2

It would look something like this:

1

class editar {
    public function selecao_editar(){
        $this::update();
    }

    function update(){
        //FAÇA ALGO
    }
}

2

class editar {
    public function selecao_editar(){
        $this->update();
    }

    function update(){
        //FAÇA ALGO
    }
}

obs: try to leave the class names in upper case. is a highly recommended good practice.

I've tested it and it worked okay here. I hope I have helped

    
19.07.2016 / 14:15
7

To call a class function inside itself you should use the $this

class foo{
    public function minhafuncao1(){
        //...
    }  
    public function minhafuncao2(){
       $this->minhafuncao1();
    }
}

Apparently you have put a space between the -> operator and the function name. You can not do this!

To call a function outside the class, you must first instantiate the class

class foo{
    private function minhafuncao1(){
        //...
    }  
    public function minhafunca2(){
       $this->minhafuncao1();
    }
}

$foo = new foo();
$foo->minhafuncao2();

Finally, the scope (: :) operator can be used in 3 ways:

class foo{
    protected function minhafuncao3(){
    }
}

class foo1 extends foo{
    public function minhafuncao1(){
        //...
    }  
    public function minhafunca2(){
       // Se referenciar a propria classe com o operador self
       self::minhafuncao1();
    }
    public function minhafuncao3(){
       // Se referenciar a herança com o operador parent, para fazer a sobrecarga
       parent::minhafuncao3();
       echo 'funcao alterada';
    }
    public static function minhafuncao4(){
       //...
    }
}
// Se referenciar a métodos estáticos     
$var = foo1::minhafuncao4();

However, it is more common to use this operator to call constants defined for the class.

    
19.07.2016 / 14:11