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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:14:49+00:00 2026-06-13T06:14:49+00:00

I have the following PHP class which contains many other app-centric functions beside my_new_iterative_function()

  • 0

I have the following PHP class which contains many other app-centric functions beside my_new_iterative_function() but when I enter the foreach the scope of $this (which I need) becomes invalid because of context. What is the correct way to pass the $this so that it is valid inside of method_foo and method_bar.

NOTE: This is part of a more complex issue, and the $fallback_order executes functions in the default sequence, but the my_new_iterative_function() needs to accept an array to control order of execution (which is the purpose of the $order_functions array.

class Foo {
    public function my_new_iterative_function(array $fallback_order = array('method_foo', 'method_bar')) {

        $order_functions = array(
            'method_foo' => function(){
                // need to access $this
            },
            'method_bar' => function(){
                // need to access $this
            },
        );

        foreach ( $fallback_order as $index => $this_fallback ) {
            $order_functions[$this_fallback]();
        }
    }
}
$instance_of_foo->my_new_iterative_function(); 
$instance_of_foo->my_new_iterative_function([ 'method_bar', 'method_foo', ]); 
  • 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-13T06:14:50+00:00Added an answer on June 13, 2026 at 6:14 am

    The easiest answer is to pass $this in as an argument:

    $order_functions[$this_fallback]($this);

    Then you would need to:

    $order_functions = array(
                'method_foo' => function($myObj){
                    // use $myObj $this
                },
                'method_bar' => function($myObj){
                    // user $myObj instead of $this
                },
            );
    

    No matter what you do you cant actually use $this inside those functions like you are inside the class instance because they aren’t part of the class instance. So you’ll need to make sure you have public accessors of some sort for all the properties or functions you need to use from the instance within those functions.

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

Sidebar

Related Questions

In PHP, I have following Singleton Database Class: class Database { private static $instance;
I have the following class in a php file: function calcTotal(){ var productID =
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>
I have the following class. Which returns certain fields. I am interested in Reference.
I have the following class in PHP class MyClass { // How to declare
I have a php page which contains a large amount of HTML in it.
I have code something like the following, which declares a class and its name
I have two PHP scripts. One acts as a backend, and contains a class
I have the following php code (getRout.php).. which response results in xml formats ...
I have the following question: This is my main file index.php . <?php class

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.