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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:09:57+00:00 2026-06-15T05:09:57+00:00

TL;DR I want to override offsetSet($index,$value) from ArrayObject like this: offsetSet($index, MyClass $value) but

  • 0

TL;DR

I want to override offsetSet($index,$value) from ArrayObject like this: offsetSet($index, MyClass $value) but it generates a fatal error (“declaration must be compatible”).

What & Why

I’m trying to create an ArrayObject child-class that forces all values to be of a certain object. My plan was to do this by overriding all functions that add values and giving them a type-hint, so you cannot add anything other than values of MyClass

How

First stop: append($value);
From the SPL:

/**
 * Appends the value
 * @link http://www.php.net/manual/en/arrayobject.append.php
 * @param value mixed <p>
 * The value being appended.
 * </p>
 * @return void 
 */
public function append ($value) {}

My version:

/**
 * @param MyClass $value
 */
public function append(Myclass $value){
    parent::append($value);
}

Seems to work like a charm.

You can find and example of this working here

Second stop: offsetSet($index,$value);

Again, from the SPL:

/**
 * Sets the value at the specified index to newval
 * @link http://www.php.net/manual/en/arrayobject.offsetset.php
 * @param index mixed <p>
 * The index being set.
 * </p>
 * @param newval mixed <p>
 * The new value for the index.
 * </p>
 * @return void 
 */
public function offsetSet ($index, $newval) {}

And my version:

/**
 * @param mixed $index
 * @param Myclass $newval
 */
public function offsetSet ($index, Myclass $newval){
    parent::offsetSet($index, $newval);
}

This, however, generates the following fatal error:

Fatal error: Declaration of
Namespace\MyArrayObject::offsetSet() must be
compatible with that of ArrayAccess::offsetSet()

You can see a version of this NOT working here

If I define it like this, it is fine:

public function offsetSet ($index, $newval){
    parent::offsetSet($index, $newval);
}

You can see a version of this working here

Questions

  1. Why doesn’t overriding offsetSet() work with above code, but append() does?
  2. Do I have all the functions that add objects if I add a definition of exchangeArray() next to those of append() and offsetSet()?
  • 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-15T05:09:58+00:00Added an answer on June 15, 2026 at 5:09 am
    abstract public void offsetSet ( mixed $offset , mixed $value )
    

    is declared by the ArrayAccess interface while public void append ( mixed $value ) doesn’t have a corresponding interface. Apparently php is more “forgiving”/lax/whatever in the latter case than with interfaces.

    e.g.

    <?php
    class A {
        public function foo($x) { }
    }
    
    class B extends A {
        public function foo(array $x) { }
    }
    

    “only” prints a warning

    Strict Standards: Declaration of B::foo() should be compatible with A::foo($x)
    

    while

    <?php
    interface A {
        public function foo($x);
    }
    
    class B implements A {
        public function foo(array $x) { }
    }
    

    bails out with

    Fatal error: Declaration of B::foo() must be compatible with A::foo($x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to override backbone.sync i have already asked this but the problem is
I want to override a string from a System.ComponentModel.DataAnnotations for an ASP.NET project. Do
I have a ListView in a XML layout but I want to override one
I want to use the Aero textbox styling, but still override some properties. I
I have a function, a() , that I want to override, but also have
I am inheriting from the PrintPreviwDialog class in c#. I want to override the
I want to override the onAttach() http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity ) method of a ListFragment from the
I want to override this method for comparing two objects. -(BOOL)isEqual:(id)object Calling function -(void)overridemethod(
I want to override a method from a module A from another module B
I want to override column names in by subclass using xml. i know this

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.