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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:52:51+00:00 2026-06-18T12:52:51+00:00

Consider this stripdown class: class foo { private $fooVar; public getFooVar(){ } } $fooObj

  • 0

Consider this stripdown class:

class foo {

    private $fooVar;


    public getFooVar(){

    }

}


$fooObj = new foo();

want i want/need to do is convert this to JSON format so I thought of putting

echo json_encode($fooObj);

however it doesnt work, i don’t have fooVar and im guessing because $fooVar is private..

any other way I can do this without having to compromise and make $fooVar public?

Thanks a bunch!

  • 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-18T12:52:53+00:00Added an answer on June 18, 2026 at 12:52 pm

    If you’re using PHP 5.3 (or earlier), you can’t really do what you’re asking for easily. You won’t get the methods encoded in JSON because JSON is a data format and doesn’t include functions, and you won’t get the property because it’s a private property. You know this much already, but it’s worth re-iterating to point out that you’re right, it’s not easy. Even writing the getter as a magic __get() method to turn it into a read-only public property won’t work in this case.

    In PHP 5.3, there are a couple of work arounds you could use.

    • Reflection. PHP includes a Reflection API, which allows you to access details about objects and classes, including reading values from private members. Obviously, however, this would mean writing a fair bit of code to get at the value, and it won’t be fast either.

    • Write the json_encode call inside the class. That way you can include private properties. I would suggest using get_object_vars($this) inside the object to get at the private properties in a JSON friendly way.

    If you’re using PHP 5.4, you have an additional option:

    • PHP 5.4 introduces a JsonSerializable interface. Add implements JsonSerializable to your class, and write a method called jsonSerialize(). This method should return the data that you want to be in the JSON string. You can include or exclude whichever public or private properties you want.

    PHP 5.5 hasn’t been released yet, but it will add another option for you:

    • PHP 5.5 will introduce a more complex syntax for defining properties, which will allow you to define properties as public but read-only, or to include more complex logic for them in the getter and setter, but still have them as properties. It will work much like the __get() and __set() methods do now, but with the variables explicitly defined as properties, which will mean that they should be accessible to the JSON serialiser, whereas currently with __get() they’re not.
      [EDIT] – note, the above paragraph is incorrect; the feature described did not make it into the PHP 5.5 release. Too early yet to say yet whether it’ll get into 5.6 or not.

    Hope that helps.

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

Sidebar

Related Questions

Consider this simple example - public class Person { private String name; private Date
Consider this: public class TestClass { private String a; private String b; public TestClass()
Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar
Consider this piece of code: http://jsfiddle.net/YttKb/ I want my javascript to add a new
Consider this example singleton class: class Model_Acl { protected static $_instance; private function __construct($a)
Consider this: require 'Twitter.class.php'; $tweet = new Twitter(username, password); foreach($comment as $key => $value)
Consider this code snippet public class ConstantFolding { static final int number1 = 5;
Consider this simple class: package net.zomis.test; public class Test { public Test() { }
Consider this class: public class Column<T> { public string Header { get; set; }
Consider this example : import java.lang.reflect.Field; public class Test { public static void main(String[]

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.