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

The Archive Base Latest Questions

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

I have some questions about the implementation of implementing ArrayAccess in PHP. Here is

  • 0

I have some questions about the implementation of implementing ArrayAccess in PHP.

Here is the sample code:

class obj implements arrayaccess {
    private $container = array();
    public function __construct() {
        $this->container = array(
            "one"   => 1,
            "two"   => 2,
            "three" => 3,
        );
    }
    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }
    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }
    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }
    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
}

Questions:

  1. I am not asking why we do have to implement ArrayAccess since I am assuming it is special interface that PHP Engine recognizes and calls the implemented inherited functions automatically?
  2. Why are we declaring the implemented function public? Since I assume they are special functions called automatically. Shouldn’t they be private since when calling saying $obj["two"] the functions are not be called from outside.
  3. Is there a special reason to assign the filled-array in __constructor function? This is the constructor function I know but in this case what kind of help it is being of.
  4. What’s the difference between ArrayAccess and ArrayObject? I am thinking the class I implemented by inheriting the ArrayAccess doesn’t support iteration?
  5. How could we implement object-indexing without implementing ArrayAccess?

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-23T02:34:30+00:00Added an answer on May 23, 2026 at 2:34 am
    1. Correct
    2. Because the interface defines them as public, therefore you have to also
    3. You don’t have to write the constructor that way if you don’t want to*
    4. ArrayAccess is an interface, ArrayObject is a class (which itself implements ArrayAccess)
    5. No other way that I’m aware of

    * Your constructor could look like this

    public function __construct( array $data ) {
        $this->container = $data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some fundamental points/questions about OpenGL, not all involving code but concepts as
I have some questions about basic CSS that I was unable to understand or
I have some questions about customers about NF mode for DB2. Google had very
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering
I have some questions about logging, more specifically about setting it up and making
I have some questions about Perl's map function. Specifically: How does %hash = map
I have some questions about partitioning and strategy, how and when to use it.
i have some questions about constructors in ColdFusion : must i use the name
I have some questions about using MySQLi queries, and related memory management. Suppose I
I have some questions about the default values in a function parameter list Is

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.