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 6725547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:49:16+00:00 2026-05-26T09:49:16+00:00

I have a question for you! Normally, if you call a callback function within

  • 0

I have a question for you!

Normally, if you call a callback function within an OOP context you have to use array(&$this, 'callback_function')

That’s what I figured out.

But now I want to call a callback in an external class, due to having to much callback_functions. I want to give them an own class for structure reasons.

I thought: “Ok, make an instance of this class and pass it instead of $this.”

So I tried it with array($cb, 'callback_function') and array($this->cb, 'callback_function') but it won’t work.

What am I doing wrong?

Thanks for your help!


Edit:

In my basic class I have:

    function __construct()
    {
        // some other vars here

        $this->cb = new Callback();
    }

And calling it with:

$newline = preg_replace_callback("/(^#+) (.*)/", array(&$this->cb, 'callback_heading'), $newline);

And in my callback class I have:

class Callback
{
    function __construct()
    {
        $this->list = array("num" => 0, "dot" => 0, "normal" => 0);
        $this->td = array("strike" => false, "bold" => false, "italic" => false, "underline" => false, "code" => false);
    }

    public function callback_heading($parameter)
    {
        $hashs = strlen($parameter[1]);
        $hashs++;
        if($hashs > 6)
            $hashs = 6;

        return "<h".$hashs."><span class=\'indented\'>".$parameter[1]."</span><strong>".$parameter[2]."</strong></h".$hashs.">";
    }
  • 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-05-26T09:49:17+00:00Added an answer on May 26, 2026 at 9:49 am

    First a comment:

    Normally, if you call a callback function within an OOP context you have to use array(&$this, 'callback_function')

    No, normally (these days) it’s array($this, 'callback_function') – without the &.

    Then, instead of $this you can put any variable that’s representing an object:

    $obj = $this;
    $callback = array($obj, 'method');
    

    or

    class That
    {
       function method() {...}
    }
    
    $obj = new That;
    $callback = array($obj, 'method');
    

    This just works, see the documentation of the callback pseudo type in the PHP Manual.


    More similar to the code fragments of your question:

    class Callback
    {
        function __construct()
        {
            $this->list = array("num" => 0, "dot" => 0, "normal" => 0);
            $this->td = array("strike" => false, "bold" => false, "italic" => false, "underline" => false, "code" => false);
        }
    
        public function callback_heading($parameter)
        {
            $hashs = min(6, 1+strlen($parameter[1]));
    
            return sprintf("<h%d><span class=\'indented\'>%s</span><strong>%s</strong></h%d>", $hashs, parameter[1], $parameter[2], $hashs);
        }
    }
    
    class Basic 
    {
        /** @var Callback */
        private $cb;
        function __construct()
        {
            // some other vars here
            $obj = new Callback();
            $this->cb = array($obj, 'callback_heading');
        }
        function replace($subject)
        {
            ...
            $result = preg_replace_callback($pattern, $this->cb, $subject);
        }
    }
    
    $basic = new Basic;
    $string = '123, test.';
    $replaced = $basic->replace($string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question regarding the use of function parameters. In the past I have
I have a ridiculous question due to a ridiculous problem. Normally if I want
I have question concerning a function I created. I would like to show the
I have a class where I use this to initialize a void* pointer. But
This seems like a fairly straightforward question, but I couldn't find this particular use-case
If I have a object, with a function that captures every form on the
I have a question about the in parameter type of a stored procedure. Normally
I understand that this is a very broad question, but I was very curious
I have a program that normally run till it terminates, but under some situations,
I have code in an svn repository, call it repository A. Now say that

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.