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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:25:05+00:00 2026-05-27T22:25:05+00:00

I was wondering if there was some way in PHP to duplicate some of

  • 0

I was wondering if there was some way in PHP to duplicate some of the magic of Python attribute/key access.

I use a Mongo ORM class written by Steve Lacey called Minimongo in which he utilizes the __getattr__ and __getitem__ to reroute key and attribute flavored access and preserve the ‘document-oriented’ nature of Mongo. val = doc.foo and val = doc['foo'] become equivalent.

I was wondering if there is a similar interface in PHP that would allow the changing of how object access is handled for a class that inherits from it. I looked through the STL and couldn’t find one that filled suit. It would be greatly useful for setting up defaults. Thanks.

  • 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-27T22:25:06+00:00Added an answer on May 27, 2026 at 10:25 pm

    Have a look at __get() and __set() and ArrayAccess.

    With the former you can make non-public members accessbile, as in $obj->foo, with the latter you can access them like $obj['foo'].

    You can hardwire them however you like, internally.

    Personally I would suggest you keep these magically-accessible properties into one single array member of the class, so you don’t end up with spaghetti code.

    POC:

     1  <?php
     2  class Magic implements ArrayAccess {
     3  
     4      protected $items = array();
     5  
     6      public function offsetExists($key) {
     7          return isset($this->items[$key]);
     8      }
     9      public function offsetGet($key) {
    10          return $this->items[$key];
    11      }
    12      public function offsetSet($key, $value) {
    13          $this->items[$key] = $value;
    14      }
    15      public function offsetUnset($key) {
    16          unset($this->items[$key]);
    17      }
    18  
    19      //do not modify below, this makes sure we have a consistent
    20      //implementation only by using ArrayAccess-specific methods
    21      public function __get($key) {
    22          return $this->offsetGet($key);
    23      }
    24      public function __set($key, $value) {
    25          $this->offsetSet($key, $value);
    26      }
    27      public function __isset($key) {
    28          return $this->offsetExists($key);
    29      }
    30      public function __unset($key) {
    31          $this->offsetUnset($key);
    32      }
    33  }
    34  
    35  //demonstrate the rountrip of magic
    36  $foo = new Magic;
    37  $foo['bar'] = 42;
    38  echo $foo->bar, PHP_EOL;//output 42
    39  $foo->bar++;
    40  echo $foo['bar'];//output 43
    41  
    

    Consistency Milord, exactly as you asked.

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

Sidebar

Related Questions

I'm doing some PHP memory benchmarks and i'm wondering if there is a way
I was wondering if there is a way to imitate PHP's magic methods __get()
I was wondering if there is some way to name or rename a property
I am wondering if there is some way to call C++ code from Common
I am wondering if there is some way to make an box have the
I am wondering if there is some way to replicate Safari (on the iPhone's)
I am wondering if there is some way to rotate a created ABPeoplePickerNavigationController. I
I am wondering if there is some way to do a doesn't equal command
I am wondering if there is some way to fade in and out audio
I am wondering if there is some way to align text on the right

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.