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

  • Home
  • SEARCH
  • 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 8744599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:51:59+00:00 2026-06-13T11:51:59+00:00

I’ve got a class Foo with public and protected properties. Foo needs to have

  • 0

I’ve got a class Foo with public and protected properties. Foo needs to have a non-static method, getPublicVars() that returns a list of all the public properties of Foo (this is just an example, I know from outside the Foo object calling get_object_vars() will accomplish this and there is no need for my getPublicVars() method).

Note: This must also return dynamically declared properties assigned at runtime to the class instance (object) that aren’t defined in the class’s definition.

Here’s the example:

class Foo{
    private $bar = '123';
    protect $boo = '456';
    public   $beer = 'yum';

   //will return an array or comma seperated list
   public function getPublicVars(){
      // thar' be magic here...
   } 
}

 $foo = new Foo();
 $foo->tricky = 'dynamically added var';

 $result = $foo->getPublicVars();  
 var_dump($result); // array or comma list with 'tricky' and 'beer'   

What is the most concise way to get the only the public properties of an object from inside a class’s own methods where both public and protected are visible?

I’ve looked at:

  • What is the best way to look inside a PHP class instance (object) to see all of its available public properties and methods?

But this doesn’t seem to address my question as it points to using get_object_vars() from outside the object.

  • 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-13T11:52:00+00:00Added an answer on June 13, 2026 at 11:52 am

    Since PHP 8.1 (Nov 2021) first class callable syntax is available to create the closure, then __invoke() it:

    return get_object_vars(...)->__invoke($this);
    

    Since PHP 7.1 (Dec 2016) creating a closure from a callable is available to create the closure, then __invoke() it:

    return \Closure::fromCallable("get_object_vars")->__invoke($this);
    

    Since PHP 7.0 (Dec 2015) a closure can be created, bound out of scope:

    return (function($object){return get_object_vars($object);})->bindTo(null, null)($this);
    

    (ikkez has a PHP 7.4 variation of it in their answer back in 2022.)

    Before PHP 7.0, the implementation-specified behaviour of call_user_func() could be used, this is Brad Kents’ example:

    return call_user_func('get_object_vars', $this);
    

    As you already realized, PHP’s build in get_object_vars is scope-sensitive. You want the public object properties only.

    So from that function to the public variant is not a large step:

    function get_object_public_vars($object) {
        return get_object_vars($object);
    }
    

    Calling this get_object_public_vars will give you only the public properties then because it is a place out of scope for the current $object.

    If you need more fine-grained control, you can also make use of the ReflectionObject:

    (new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC);
    

    Which has the benefit that you don’t need to introduce another function in the global namespace.


    The function create_user_func() shouldn’t be used (because eval() and IIRC each time a new function was created). Luckily, since PHP 5.3 (Jun 2009) there are anonymous functions.

    However, it is a correct answer to the question given when jumc answered in Apr 2013 Brad Kents’ hack was not there yet and no other answer existed to show changing the scope of the closure that requires PHP 5.4 (Mar 2012; "Implemented closure rebinding as parameter to bindTo." ref).

    Therefore, for completeness, the PHP 5.4 syntax closure example:

    $scope = array ( function ($object) { return get_object_vars($object); }, 'bindTo' );
    return call_user_func($scope(null, null), $this);
    

    as the alternative form of:

    $scope = create_function('$object', 'return get_object_vars($object);');
    return $scope($this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.