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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:34:38+00:00 2026-05-31T00:34:38+00:00

I can’t quite understand why the output of this code is ‘1’. My guess

  • 0

I can’t quite understand why the output of this code is ‘1’.
My guess is that php is not behaving like most other OO languages that I’m used to, in that the arrays that php uses must not be objects. Changing the array that is returned by the class does not change the array within the class. How would I get the class to return an array which I can edit (and has the same address as the one within the class)?

<?php
    class Test
    {
        public $arr;
        public function __construct()
        {
            $this->arr = array();
        }

        public function addToArr($i)
        {
            $this->arr[] = $i;
        }

        public function getArr()
        {
            return $this->arr;
        }
    }

    $t = new Test();
    $data = 5;
    $t->addToArr($data);

    $tobj_arr = $t->getArr();
    unset($tobj_arr[0]);

    $tobj_arr_fresh = $t->getArr();
    echo count($tobj_arr_fresh);
?>

EDIT: I expected the output to be 0

  • 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-31T00:34:40+00:00Added an answer on May 31, 2026 at 12:34 am

    You have to return the array by reference. That way, php returns a reference to the array, in stead of a copy.

    <?php
        class Test
        {
            public $arr;
            public function __construct()
            {
                $this->arr = array();
            }
    
            public function addToArr($i)
            {
                $this->arr[] = $i;
            }
    
            public function & getArr() //Returning by reference here
            {
                return $this->arr;
            }
        }
    
        $t = new Test();
        $data = 5;
        $t->addToArr($data);
    
        $tobj_arr = &$t->getArr(); //Reference binding here
        unset($tobj_arr[0]);
    
        $tobj_arr_fresh = $t->getArr();
        echo count($tobj_arr_fresh);
    ?>
    

    This returns 0.

    From the returning references subpage:

    Unlike parameter passing, here you have to use & in both places – to
    indicate that you want to return by reference, not a copy, and to
    indicate that reference binding, rather than usual assignment, should
    be done

    Note that although this gets the job done, question is if it is a good practice. By changing class members outside of the class itself, it can become very difficult to track the application.

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

Sidebar

Related Questions

Can someone please explain how this code is working for me? I don't understand
Can someone maybe tell me why this is not working? I have used echo
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can I write this with less code, also is .click(function preferred for mobile or
Can anyone tell me how an IDE like NetBeans or any for that matter
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can anyone explain to me why this program: for(float i = -1; i <
I've got a string that has curly quotes in it. I'd like to replace
Can I call select before recv_from on a socket that is blocking?
Can anybody tell me a regular expression to use within some PHP to find

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.