php callback function in class not being called - php

Given this line of code to callback a function and the callback function itself:
Class MyClass {
public function doThings() {
$this->gearman->client->setCompleteCallback("workerCompleted");
}
public function workerCompleted() {
echo "worker is completed!";
}
}
I get Message: GearmanClient::setCompleteCallback(): function workerCompleted is not callable
The setCompleteCallback method expects a function name in quotations, but how do I call a class method? $this->"workerCompleted" obviously is wrong.

$this->gearman->client->setCompleteCallback("workerCompleted");
should be
$this->gearman->client->setCompleteCallback( array( $this, "workerCompleted"));
Because you're specifying a callback to a member function, which requires an array of the object (or classname) that needs to be called, along with the name of the function. More info at the docs.

Maybe you used the doBackground function to send the code the gearman server? That indeed doesn't use the Callback, since you by definition don't care about the job status and results in your application code.

Related

Calling php functions that have an instance as an argument

Ok so my question is, i have a Facebook call back function that I'm using. The callback function uses an instance as an argument facebookCallBack(LaravelFacebookSdk $fb)
The function works great,I'm using this in my auth controller in larvel , but now I want to reuse this function within the same controller something like $this>facebookReusableCallBack(LaravelFacebookSdk $fb)
I'm having issues ,i get this error facebookReusableCallBack() must be an instance of SammyK\LaravelFacebookSdk\LaravelFacebookSdk, none given.
here's a sample of my code:
public function facebookCallBack(LaravelFacebookSdk $fb) {
$this->facebookReusableCallBack();
}
public function facebookReusableCallBack() {
//All my code here
}
how can I reuse this function within my controller?
i've tried :
public function facebookCallBack(LaravelFacebookSdk $fb) {
$this->facebookReusableCallBack(LaravelFacebookSdk $fb);
}
but the argument doesn't get passed as an instance?
You don't need to define the type of a variable when you pass it in.
Method definition:
public function facebookReusableCallBack(LaravelFacebookSdk $fb) {
//some code
}
Execution within another method:
function myMethod() {
$var = new LaravelFacebookSdk();
$this->facebookReusableCallBack($var);
}
What you have done is define the type of the variable in your function definition (good) to force the type. However these are only needed in the definition.
So for your explicit example:
public function facebookCallBack(LaravelFacebookSdk $fb) {
$this->facebookReusableCallBack($fb);
}
Because $fb must be an instance of LaravelFacebookSdk when calling facebookCallBack it will be fine for facebookReusableCallBack.

Calling a method outside class statically, passing other method's output as parameter

I have a class like this:
class myClass {
public static function load()
{
return new self();
}
public function myMethod1 ()
{
return 'content';
}
public function myMethod2 ($content)
{
return 'modified '.$content;
}
}
Somewhere in my script, I have to call myMethod2 using myMethod1's output as parameter.
The way I currently do that, is:
echo myClass::load()->myMethod2(myClass::load()->myMethod1()); // modified content
It works, but I'm pretty confident there is a more appropriate way... So my questions:
Is there a proper way of passing method's output as other method's parameter in a class loaded "statically"?
Besides that, there is a more elegant code structure to accomplish the same "task"?
Thanks!
(This is my first question in StackOverflow, I apologize for mistakes, eventually)

unserialize() ... Function spl_autoload_call() hasn't defined the class it was called for

I'm using factory to create an object and the static method to unserialize this object:
public static function factory($idText) {
$fetchedObject = self::fetchStoredObject($idText);
return $fetchedObject;
}
private static function fetchStoredObject($idText) {
$fetchedText = DB::select()
->from(table)
->where('idText', '=', $idText)
->execute()->as_array();
if (!empty($fetchedText)) {
return unserialize(base64_decode($fetchedText[0]['txtContent']));
} else {
return NULL;
}
}
The object is created in this way:
$text = Article::factory($idText);
But I get the following error:
unserialize() [<a href='function.unserialize'>function.unserialize</a>]:
Function spl_autoload_call() hasn't defined the class it was called for
On this line of fetchStoredObject method:
return unserialize(base64_decode($fetchedText[0]['txtContent']));
Why does this error occur?
EDIT
My class has the following structure:
class Article {
private $phpMorphy;
private $words; //contains instances of class Word
...
private function __construct($idText) {
$this->initPhpMorphy(); // $this->phpMorphy gets reference to the object here
}
public function __sleep() {
return Array(
'rawText',
'idText',
'properties',
'words',
'links'
);
}
public function __wakeup() {
$this->initPhpMorphy();
}
}
The Word class doesn't contain the reference to phpMorphy as own property, but uses it in its methods as function parameter.
Here is the part of the serialized string:
" Article words";a:107:{i:0;O:4:"Word":10:{s:5:" * id";O:9:"phpMorphy":7:{s:18:" * storage_factory";O:25:
It appears the phpMorphy is serialized with connectrion to the Word class. Am I right?
The error occurs because inside your serialized string there is a reference to a class that hasn't been included yet - so the PHP autoloading mechanism is triggered to load that class, and this fails for some reason.
Your steps for debugging would be:
Identify which class is included in that serialized string.
Check if you have code for that class somewhere.
Make sure this code can be loaded via autoloading. Alternatively make sure that code is included before unserializing.
What version of PHP did you use? Where did you store your Article class? Try to require() it manually.
The problem is fixed thanks to Sven suggestions. The object of class Word (part of the class Article) contained reference to the phpMorphy class (which happened because I changed parameters order when creating an instance of the word!).

Call function of a class within a function in the function within the same class

From the function abc();1. How do I call ‘function a within class A’? ($this->a(); returns error)?
2. How do I access to public variable $bbb?
(- I know the structure is bad but ‘require_once’ part is dynamic etc…)
class AAA extends CI_Controller
{
public $bbb;
function ccc ()
{
}
function index ()
{
require_once '1.php';
}
}
// in 1.php
function abc ()
{
// how do i call method a of Class A?
$this->ccc(); // returns error - Using $this when not in object context in ...
$this->bbb; //
}
abc(); // etc etc
You're trying to do something with require() which it is not designed to do, and doesn't work for.
However, all functions and classes defined in the included file have the global scope.
http://us.php.net/manual/en/function.include.php which also also applies to require().
abc() gets defined in the global scope and thus doesn't have the object context necessary to use $this.
As amber mentioned require wont help here. Couldnt you just pass reference to the function?
function abc (&$ref)
{
$ref->ccc();
$ref->bbb;
}
require it outside of class normally ant then just call
function index ()
{
abc($this);
}
not sure it will work, but worth a try i believe

PHP Methods that are always called

I'm currently working on an own PHP-MVC-Framework (for experience purposes only).
My question: Is it possible to call a defined function or method, every time a class-method
has been called?
For example:
public function view($id) {
//Code ...
$this->view->render(__FUNCTION__);
}
What I want is:
public function view($id) {
//Code ...
//render-method is called automatically with functionname as parameter
}
I tried different methods ... but without success.
Would be great if someone could help me out with this.
Cheers,
Chris
You can use Magic Methods do achieve this behavior:
public function __call($func, $args) {
if(!method_exists($this, $func)) {
return;
}
// do some coding here
call_user_func_array($func,$args);
// do some coding there
}
private function view($arg1, $arg2) {
// and here
}
Remember: view function must be private/protected.
$obj->view("asdasd", "asdsad");
Should do ::__call(), then ::view() method
You could create a function as a liaison using PHP's ability to use variable values for execution purposes. for example:
function call($func,$param)
{
$this->$func($param);
$this->render($func);
}
$myObj->call('view',$id);
You can use a wrapper method. Call this method and pass everything else as a parameters.

Categories