Equivalent to PHP eventListener

Asked

Viewed 105 times

0

I have a series of PHP classes that assemble my HTML structures based on CSS Bootstrap already added with their respective Javascript actions, such as this call:

$VIDB_button = new \vidbModel\buttonElement();
$btn = $VIDB_button->color('primary')->content('Salvar')->addicionalClassnames('pull-right exec-save-new-company')->event('submit_fake_form')->calls('caller|classname|Classe|metodo')->actions('closeClosestModalOnSuccess|primary-modal|SweetAlertByResponse')->dataType('json')->event_post_content(null)->type('button')->button(0);

This method makes a button with the actions of submitting the form that it is inside, already indicate the concatenated data to the POST, indicate the JS method that will close the modal that the form is inside, and reply in a Sweetalert the result, indicates that and response should be json, and the calls method indicates which PHP methods should be called when submitting the form.

Well, I have a small problem is that the methods need to be initiated in a specific order for it to work, that is, if I change anything in the order of the call something does not work right.

I would like to know how to 'hear' that a method has been called, in order to make the cadence mandatory and return an error if something is out of place, ex:

'Hear that class button was called 'or 'hear that method calls class buttons was called before event, and thus inform on a error the correct order', something like the hypothetical function to follow:

class classe {

    public function observar(){

        if(ClasseAnterior::metodo_observado() == 'foi_chamado'){
            \\verificar a execução do metodo_observado(), poder executar algo antes dele, e depois confirmar a execução do mesmo 
        }

    }

}

There is something 'natural' from PHP to monitor/hear method triggers and classes?

If not, how could something related be created ?

If there is no way to create something related, what would be a viable option to have a similar result ?

  • 1

    Have you considered using Splobserver and the Splsubject? Can implement an event class in a good way easy also. Finally, perhaps the debug_backtrace help you check the order of the calls, but I believe a simple Observer with a sequence validation should solve.

  • @Tommelo can make a minimal example of an observation ?

  • 1

    Of course, here is a very basic example: https://gist.github.com/tommelo/e9ca08d875e4739a5bff92b8b90bfe2f. In your case, you need to implement the sequence validation rules of calls in each instance of the class you want to observe. It’s gonna take a little more work, but I don’t think it’s too complex.

  • @Tommelo took a look here, it’s really simple, it’s only massive to do, but it will be good, if you want to make an answer to accept, I believe that already solved what I wanted to do.

  • 1

    I’ll leave this to some PHP expert to answer, sure that you can implement something easier and elegant, I have little time to research further. Success in the implementations ae!

No answers

Browser other questions tagged

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