Jump to content
iwato

Chaining PHP Class Methods

Recommended Posts

BACKGROUND:  While implementing Peppe Occhi's CRON Scheduler I have come across several lines of code that I am not entirely familiar.  In particular I am concerned with the following:

$scheduler->php('script.php')->hourly();

found under the heading Schedules execution time. It appears to be of this format

$some_instance->some_methodA(arguments)->method_B(arguments);

QUESTION:  Is the above expression equivalent to the following:

$some_instance->some_methodA(arguments);
$some_instance->method_B(arguments);

In other words, the following is correct:

$scheduler->php(
	'path_to_folder/script.php',
	'path_to_php_version/ea-php72',
	['-c' => 'ignore','--merge' => null,],
	'file_name'
);
$scheduler->hourly();

Please advise.

Roddy

 

 

Share this post


Link to post
Share on other sites

No, it's not equivalent, there are actually two objects there: One is the scheduler and the other is whatever thing the scheduler's php() method is returning.

The above code is equivalent to this:

$thing = $scheduler->php('script.php');
$thing->hourly();

I don't know what type of object $thing is, since I haven't read the documentation.

The concept of chaining is not native to any language, it only happens if somebody designed the objects to work that way.

  • Thanks 1

Share this post


Link to post
Share on other sites

FOLLOW-UP QUESTION:  In light of the above does the following make sense:

$matomo_api = new Scheduler();
	
$matomo_freq = $matomo_api->php('.../fetch_and_store.php','.../bin/ea-php72',['-c' => 'ignore','--merge' => null,],'matomo_api');

$matomo_freq->hourly();
$matomo_api->run();

Roddy

 

Share this post


Link to post
Share on other sites

That will probably work. You should test it to make sure. Run the code and see if it does what you expected it to.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×