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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:23:47+00:00 2026-05-28T02:23:47+00:00

My Chart class actually allows me to create simple properties (of type string ,

  • 0

My Chart class actually allows me to create simple properties (of type string, boolean and so on) as well as nested object properties calling the magic __call method this way:

$chart = new Chart();
$chart->simple = 'Hello';
$chart->newComplex();

var_dump($chart);

Output:

object(Chart)[1]
  public 'simple' => string 'Hello' (length=5)
  public 'complex' => 
    object(stdClass)[2]

I’d like to add the ability to create also nested object properties as children of other properties (not children of chart itself) in a way like this:

$chart->newComplex2($chart->newComplex1());

Question is: how to use $args parameter and modify __call() to accomplish this?

class Chart
{

    public function __call($name, $args)
    {

        $type  = substr($name, 0, 3);
        $field = lcfirst(substr($name, strlen($type)));

        switch($type)
        {
            case 'get': return isset($this->$field) ? $this->$field : null;
            case 'new': return $this->$field = new stdClass();
        }

    }

}
  • 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-05-28T02:23:48+00:00Added an answer on May 28, 2026 at 2:23 am

    I found solution by myself. The trick is to pass the parent property to __call. Here is the code:

    public function __call($name, $args)
    {
    
        $type  = substr($name, 0, 3);
        $field = lcfirst(substr($name, strlen($type)));
    
        switch($type)
        {
            case 'get':
                return isset($this->$field) ? $this->$field : null;
            case 'new':
                return (isset($args[0]) ? $args[0]->$field = new stdClass()
                    : $this->$field = new stdClass());
            default: return $this->$name();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class String { private: char* rep; public: String (const char*); void toUpper() const; };
I get from another class string that must be converted to char. It usually
I have: class Foo { int a; int b; std::string s; char d; };
I have a simple has_many/belongs_to relationship between Report and Chart. The issue I'm having
Say, I want to create a File class class File{ public: File(const char *file){
I've got a String class. Recently, I found the poor thing couldn't handle: String
I am trying to create a scatter chart on my web page with CSS
I have SqlDataReader that was sitting in a page's code behind (well actually a
§27.7.3.9 defines the following overload for operator<< : template <class charT, class traits, class
iostream and other stream class are not actually class, but typedef s, right? Here

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.