Automatic method execution

1

I have a class x that needs to execute a method y once a day. However, how to do this in PHP Object Oriented?

Q: In structured PHP, I used crontab or fcron to schedule a particular URL to run at a given time. In that script, I would put the necessary modifications.

    
asked by anonymous 23.06.2014 / 13:49

3 answers

2

Just point to this scheduled URL a file with this class's method call

Class File:

<?php

// path/minhaClasse.php

class minhaClasse{
    public function meuMetodo($parametro){
        // Faz alguma coisa
    }

    public static function meuMetodoEstatico($parametro){
        // Roda o mesmo método, porém de forma estática
        return $this->meuMetodo($parametro);
    }
}

File with class call

<?php
require "path/minhaClasse.php";

$classe = new minhaClasse();
$classe->meuMetodo('parametro');

Or you can use a static call:

<?php
require "path/minhaClasse.php";

\minhaClasse::meuMetodoEstatico('parametro');
    
23.06.2014 / 14:07
1

You will still have to run cron to schedule the php call because php depends on a request to be interpreted.

You can write a class in PHP and use the methods class_exists to check if the class x exists, if it exists you create an instance of it and use the method, method_exists to verify that the and method exists. If it exists, you execute the method.

You can use php as a script to call it in terminal via cli, you just have to say that the text interpreter to be used is php, adding a statement similar to the statement after the start of the file, because it should have the path of your php interpreter, if you do not know where to find it, you can type it on the whereis php console, which will be displayed).

#!/usr/local/bin/php

I'm assuming you're using linux.

    
23.06.2014 / 14:15
0

An unusual way of solving your problem without using CRON would be with a script that would loop an infinite loop with the following content (or something like this):

<?php

while(true){
    checkRemoteSite();
    updateYourDB();
    sleep(6000);
}
    
23.06.2014 / 15:46