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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:41:48+00:00 2026-06-13T10:41:48+00:00

I have a php class Assets . Within Assets there are a variety of

  • 0

I have a php class Assets. Within Assets there are a variety of public functions which handle assets (caching, minifying, combining…). One of the public functions contains a secondary function which is required to perform a preg_replace_callback(). This inner function needs to access one of the other public functions but I am having trouble calling the other functions.

Here is the setup:

class Assets
{

    public function img($file)
    {

        $image['location'] = $this->image_dir.$file;
        $image['content'] = file_get_contents($image['location']);
        $image['hash'] = md5($image['content']);
        $image['fileInfo'] = pathinfo($image['location']);

        return $this->cache('img',$image);

    }

    public function css($content)
    {

        . . .

        function parseCSS($matched){

            return $this->img($matched); // THIS LINE NEEDS TO REFERENCE function img()

        }

        $mend = preg_replace_callback(
            '#\<parse\>(.+?)\<\/parse\>#i',
            'parseCSS',
            $this->combined_css
        );

        . . .

    }

}

Here’s what I have tried:

$this->img($matched)

Error: Using $this when not in object context – Refers to $this->
inside of parseCSS()

Assets::img($matched)

Error: Using $this when not in object context – Refers to $this->
inside of img()

So, how can I access a public function with $this from within an inner function?

  • 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-13T10:41:49+00:00Added an answer on June 13, 2026 at 10:41 am

    This would be more appropriate:

    public function css($content)
    {
        //. . .
        $mend = preg_replace_callback(
            '#\<parse\>(.+?)\<\/parse\>#i',
            array($this, 'parseCSS'),
            $this->combined_css
        );
        //. . .
    }
    
    public function parseCSS($matched){
        return $this->img($matched); // THIS LINE NEEDS TO REFERENCE function img()
    }
    

    Your original approach causes parseCSS to be defined every time css is called — which will probably result in a fatal error were you to ever call css twice. All questions of scope are also much more straightforward in my revised example. In your original example, parseCSS is a function in the global scope, and not associated with your class.

    Edit: Valid callback formulations are documented here: http://php.net/manual/en/language.types.callable.php

    // Type 1: Simple callback
    call_user_func('my_callback_function'); 
    
    // Type 2: Static class method call
    call_user_func(array('MyClass', 'myCallbackMethod')); 
    
    // Type 3: Object method call
    call_user_func(array($obj, 'myCallbackMethod'));
    
    // Type 4: Static class method call (As of PHP 5.2.3)
    call_user_func('MyClass::myCallbackMethod');
    
    // Type 5: Relative static class method call (As of PHP 5.3.0)
    call_user_func(array('B', 'parent::who')); // A
    
    //Type 6: Closure
    $double = function($a) {
        return $a * 2;
    };
    
    $new_numbers = array_map($double, $numbers);
    

    A closure-based solution is also possible as of PHP 5.4 — this would actually be similar to what you originally intended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a PHP class such as this one: class A { public
Here's the objective. I have a PHP class and there are one or two
I have one PHP class thus: class DB extends mysqli{ public function __construct( {
I have a PHP class ( POJO_FOO ) which maps to a table (
I have a PHP class with a static function: <? echo view::getUserSelector() ?> Which
i have next php code <?php class A { public function foo() { }
I have a PHP Class which requires a unique value in its constructor. If
I have a php class, RegistrationController, <?php class RegistrationController extends ParentController { function __construct()
I have certain PHP class methods that access external variables. These variables are not
I have a PHP class that stores a complex multidimensional array, and rather than

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.