Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:42:40+00:00 2026-06-03T22:42:40+00:00

I am trying to use an instance method as a callback for PHP 5.2.1.

  • 0

I am trying to use an instance method as a callback for PHP 5.2.1. I am aware that as of PHP 5.4 you can use $this inside a closure, and in PHP 5.3 you can rename $this to $self and pass that to the closure. However, neither of these methods will suffice since I need this to work for PHP 5.2.1. The two commented lines was my last attempt. That results in Fatal error: Call to a member function hello() on a non-object – is there anyway I can have a callback to an instance method in PHP 5.2.1?

<?php

class Test {

    public function __construct() { 
        $self = &$this;

        $cb = function() use ( $self ) {
            $self->hello();
        };
        call_user_func( $cb );


        // $cb = create_function( '$self', '$self->hello();' );
        // call_user_func( $cb );

    }

    public function hello() {
        echo "Hello, World!\n";
    }
}

$t = new Test();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:42:42+00:00Added an answer on June 3, 2026 at 10:42 pm
    $cb = create_function('$self', '$self->hello();');
    

    This is just making a function that can take a parameter called $self. It’s the same as this:

    function test($self){
        $self->hello();
    }
    

    You can try passing $self (or $this) to the function when you call it:

    call_user_func($cb, $this);
    

    You can also try to make $self a global variable, so that the anonymous function made by create_function can read it.

    $GLOBALS['mySelf'] = $self;
    $cb = create_function('', 'global $mySelf;  $mySelf->hello();');
    call_user_func($cb);
    // You may want to unset this when done
    unset($GLOBALS['mySelf']);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use this method (gist of which is use self.method_name in the FunnyHelper
you can't use string.Remove inside a static method because Remove is an instance method.
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying to use the mutex method for only allowing one instance of my
I am trying to use the IMFSourceResolver::CreateObjectFromByteStream method to create a IMFMediaSource instance for
I have an instance method that populates a vector of strings. I am trying
I am trying to understand how to use instance variable in Perl OO -
I'm trying to use cx_Oracle to connect to an Oracle instance and execute some
I am trying to use the Class.forName('com.mypack.MyDomain').newInstance() to create an instance of the grails
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.