Perform function from the same class

Asked

Viewed 1,898 times

-1

I have tried several forms here on the site and on the net, no more loads the other function. 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’ve 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(){
        ...
    }
}

2 answers

8

to call a class function within itself you must use the operator $this

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

Apparently you put a gap between the operator -> and the function name. You cannot do this!

To call a function from 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 resolution 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 defined constants for the class.

2


It would look something like this:

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

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

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

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

Obs: try capitalizing class names. It is a highly recommended good practice.

Already tested and worked ok here. I hope to have helped

  • Give me the following error when I click on the form inside the first function. Fatal error: Call to Undefined method edit::update() in

  • This means you are still using edit ::update();. Use $this::update();

  • Worse was right. I’m using as I said, and the error continues

  • The error mentioned says that somewhere you are doing it edit ::update(). That there is no you have to find where it is.

  • I found the bug. Plus the first function contains a POST for the second function. And it does not execute.

  • You need to pass as a function parameter in this case.

  • So, I can call the other function, plus if I put if (isset($_POST['Edit'])) {...} in the second function does not perform.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.