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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:22:20+00:00 2026-06-13T09:22:20+00:00

Okay, so this has been baking my noodle for a little while now. I

  • 0

Okay, so this has been baking my noodle for a little while now. I have a class with a method that defines a globally accessible function. My question is: how is it possible for an internal class method to define a function that is accessible at the global scope?

Here’s a sample:

class MyClass 
{

    // ... accessors, constuctors other methods, et al... 

    // The method in question:
    private function myPrivateMethod()
    {
        if( !function_exists( 'someArbitraryFunction' ) )
        {
            function someArbitraryFunction( $args )
            {
                return "Hello, {$args} world!";
            }
        }            
    } 
}

The class is instantiated as usual, and very early on in the application, but it is instantiated within another class’s method. It’s a shallow scope-chain, but nested enough that it doesn’t make sense (to me) why it would be accessible outside of the application. This goes against my understanding of encapsulation, some insight would be much appreciated.

  • 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-13T09:22:21+00:00Added an answer on June 13, 2026 at 9:22 am

    Functions declared will always have a global scope. This is not JavaScript.

    See:

    class MyClass 
    {
    
        // ... accessors, constuctors other methods, et al... 
    
        // The method in question:
        private function myPrivateMethod()
        {
            if( !function_exists( 'someArbitraryFunction' ) )
            {
                function someArbitraryFunction( $args )
                {
                    return "Hello, {$args} world!";
                }
            }            
        } 
    
        public function run(){
            $this->myPrivateMethod();
        }
    }
    
    var_dump(function_exists('someArbitraryFunction')); // false
    $obj = new MyClass();
    $obj->run();
    var_dump(function_exists('someArbitraryFunction')); // true
    

    Being a language from a C/C++ background, there is a distinct behaviour between functions and methods. In PHP, functions do not obey the access modifiers.

    Regardless of where they are written in the code, once the execution runs through the declaration, it becomes defined.

    If you need a function to obey scoping, which I highly recommend because you get to utilize the GC of variables better, you can use Closures in >= PHP 5.3:

    class MyClass{
    
        private $myArbitraryMethod;
    
        private function myPrivateMethod(){
            $this->myArbitraryMethod = function($args){
    
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, now I've been using drawImage in java for a while, and this has
Okay i know this has been asked a lot but it seems that none
Okay, this has been absolutely infuriating to debug, I have finally figured out what
Okay, I know this question has been asked before: Previous Question I have also
Okay so i have been working on this app for a couple months now
Okay this one has me stumped.. mainly because i have been working on this
Okay - this is the dumbest glitch I have seen in a while: <!DOCTYPE
Is it okay( and legal) to delete a pointer that has been passed as
Okay, i know there have been some questions about this before, but I just
I know this topic has been discussed a lot , but I have a

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.