Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use closures inside a class #345

Open
igorlealantunes opened this issue Jan 27, 2021 · 3 comments
Open

Use closures inside a class #345

igorlealantunes opened this issue Jan 27, 2021 · 3 comments

Comments

@igorlealantunes
Copy link

Crunz version: 2.0+

PHP version: 7.4.9

Operating system type and version: MAC OSX

Description
I am trying to run my tasks inside a class function (instead of plain PHP scripts). And I'm getting errors related to class namespaces (some internal classes of the library cannot access the class that I am using to wrap the call to the closure inside the run() method of the scheduler object ).

How to reproduce
Try to call the run() method of the schedule object inside a class.

Possible Solution
Remove the dependencies between the closure function passed into the run() method and the parent class.

Additional context

Example of usage with classes:

<?php 

namespace Services\Cron_jobs;

/**
 *  Organize each cron job inside a class
 */
class Test_task 
{
	function __construct()
	{
		$this->schedule = new \Crunz\Schedule();
	}

	public function run() # each class of type "task" will have a run method (we can create interfaces or abstract class to ensure the contract)
	{	
		$task = $this->schedule->run(function() {
			file_put_contents("test.txt", "Testing only");
		});

		$task
			->everyMinute()
			->description('Testing only');    

		return $this->schedule;		
	}
}

return (new Test_task())->run(); # returns an instance of \Crunz\Schedule(); as required by the library
@PabloKowalczyk
Copy link
Collaborator

Hello, do you use Composer and Crunz shares autoloader with your app?

@igorlealantunes
Copy link
Author

Hi Pablo, thanks for the support.

I am using Composer to install Crunz, but the autoloader is not the same of my app.

@PabloKowalczyk
Copy link
Collaborator

Could you try to somehow "merge" autoloaders and then test? Looks like issue is not with Crunz but with autoloader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants