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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:52:47+00:00 2026-05-16T13:52:47+00:00

Is there a way to make a read-only property of an object in PHP?

  • 0

Is there a way to make a read-only property of an object in PHP? I have an object with a couple arrays in it. I want to access them as I normally would an array

echo $objObject->arrArray[0];

But I don’t want to be able to write to those arrays after they’re constructed. It feels like a PITA to construct a local variable:

$arrArray = $objObject->getArray1();
echo $arrArray[0];

And anyways, while it keeps the array in the object pristine, it doesn’t prevent me from re-writing the local array variable.

  • 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-16T13:52:48+00:00Added an answer on May 16, 2026 at 1:52 pm

    Well, the question is where do you want to prevent writing from?

    The first step is making the array protected or private to prevent writing from outside of the object scope:

    protected $arrArray = array();
    

    If from "outside" of the array, a GETTER will do you fine. Either:

    public function getArray() { return $this->arrArray; }
    

    And accessing it like

    $array = $obj->getArray();
    

    or

    public function __get($name) {
        return isset($this->$name) ? $this->$name : null;
    }
    

    And accessing it like:

    $array = $obj->arrArray;
    

    Notice that they don’t return references. So you cannot change the original array from outside the scope of the object. You can change the array itself…

    If you really need a fully immutable array, you could use a Object using ArrayAccess…

    Or, you could simply extend ArrayObject and overwrite all of the writing methods:

    class ImmutableArrayObject extends ArrayObject {
        public function append($value) {
            throw new LogicException('Attempting to write to an immutable array');
        }
        public function exchangeArray($input) {
            throw new LogicException('Attempting to write to an immutable array');
        }
        public function offsetSet($index, $newval) {
            throw new LogicException('Attempting to write to an immutable array');
        }
        public function offsetUnset($index) {
            throw new LogicException('Attempting to write to an immutable array');
        }
    }
    

    Then, simply make $this->arrArray an instance of the object:

    public function __construct(array $input) {
        $this->arrArray = new ImmutableArrayObject($input);
    }
    

    It still supports most array like usages:

    count($this->arrArray);
    echo $this->arrArray[0];
    foreach ($this->arrArray as $key => $value) {}
    

    But if you try to write to it, you’ll get a LogicException…

    Oh, but realize that if you need to write to it, all you need to do (within the object) is do:

    $newArray = $this->arrArray->getArrayCopy();
    //Edit array here
    $this->arrArray = new ImmutableArrayObject($newArray);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make the UIPickerView read-only? This means that the user
If I have a read-only property on an object that fills itself via the
Is there a way to make my mysql and php server automatically display dates
What is the best way to make a class property Write Once, Read Many
Let's say I have a Concrete class with a Read-Only property e.g. public class
UIImage has a read-only property CGImage. I have to read its pixels to a
I have Class1 with a read-only bindable property called age : public class Class1
Is there a way to make a button's background color change from a color
Is there a way to make MySQL cause some kind of error when doing
Is there a way to make Timeline start recording EXACTLY at the time where

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.