Code Monkey home page Code Monkey logo

Comments (5)

liebig avatar liebig commented on July 16, 2024

Hi, yes, [...] within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. Please have a look at the Variable scope documentation.
This should help:

use Illuminate\Support\Facades\Log;
...
while(someloop){
	$variable_from_outside=0;
	$variable_from_outside++;
	\Cron::add('samplecron', '* * * * *', function() {
                global $variable_from_outside;
		Log::info('$variable_from_outside: '.$variable_from_outside);
	}, false);
}

or maybe

use Illuminate\Support\Facades\Log;
...
while(someloop){
	$variable_from_outside=0;
	$variable_from_outside++;
	\Cron::add('samplecron', '* * * * *', function(&$variable_from_outside) {
		Log::info('$variable_from_outside: '.$variable_from_outside);
	}, false);
}

This Stackoverflow entry could help, too.

from cron.

franzlocksoftwares avatar franzlocksoftwares commented on July 16, 2024

Hmm.. ive tried both solution just now.. seems theyre still not working as expected..
1st solution turns out to be just initializing the variable inside the function..
2nd solution gives this error
image

we've also tried to use private class variables, but still no luck. i have some idea now on how i could pass an outside variable, but it includes some modification in the codes within the package(which is as much as posible i would like to avoid.).

from cron.

liebig avatar liebig commented on July 16, 2024

Ah I see. The second solution does not work, because I call the function without Parameters ($return = $job['function']();). Please try the use keyword.

use Illuminate\Support\Facades\Log;
...
while(someloop){
	$variable_from_outside=0;
	$variable_from_outside++;
	\Cron::add('samplecron', '* * * * *', function() use ($variable_from_outside) {
		Log::info('$variable_from_outside: '.$variable_from_outside);
	}, false);
}

from cron.

franzlocksoftwares avatar franzlocksoftwares commented on July 16, 2024

ah thats it.. it works now! i ddint even know that was possible.. thanks!
im so sorry, its my first time encountering this kind of problem and solution. but thanks a lot, its a new learning for me..

from cron.

liebig avatar liebig commented on July 16, 2024

You are welcome :)

from cron.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.