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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:51:22+00:00 2026-05-15T08:51:22+00:00

I’ve created a short demonstration of the problem I’m having. This isn’t exactly how

  • 0

I’ve created a short demonstration of the problem I’m having. This isn’t exactly how I’m implementing it but seems to lead to the same result.

<?php

class mainclass {

    var $vardata = array();

    function &__get ($var) {
        if ($this->vardata[$var]) return $this->vardata[$var];
        if ($var == 'foo') return $this->_loadFoo();
        return NULL;
    }

    function __set ($var, $val) {
        $this->vardata[$var] = $val;
    }

    function __unset($var) {
        unset($this->vardata[$var]);
    }

}

class extender extends mainclass {

    function __construct() {

        var_dump($this->foo);
        $this->_loadFoo();
        echo '<br>';
        var_dump($this->foo);

    }

    function _loadFoo () {

        unset($this->foo);
        $this->foo = array();

        $this->foo[] = 'apples';
        $this->foo[] = 'oranges';
        $this->foo[] = 'pears';

        return $this->foo;

    }

}


$test = new extender;

?>

The output of the above code is:

array(3) { [0]=>  string(6) "apples" [1]=>  string(7) "oranges" [2]=>  string(5) "pears" }
array(5) { [0]=> string(6) "apples" [1]=> string(7) "oranges" [2]=> string(5) "pears" [3]=> string(7) "oranges" [4]=> string(5) "pears" } 

Where as I was expecting:

array(3) { [0]=>  string(6) "apples" [1]=>  string(7) "oranges" [2]=>  string(5) "pears" }
array(3) { [0]=>  string(6) "apples" [1]=>  string(7) "oranges" [2]=>  string(5) "pears" }

The __get, __set and __unset functions are all being called in the right places and so I’d have expected the second, direct calling of the function to simply unset $this->foo and fill it again with the same data. Meaning that var_dumping it would give the same output. Instead, well, it ends up doing the above… filling it with two of the three strings directly set and keeping the first three originally set strings.

Perhaps just a silly mistake or a misunderstanding of the overloading functions – either way, I’ve been stuck here for too long now and so any help is much appreciated!

  • 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-15T08:51:23+00:00Added an answer on May 15, 2026 at 8:51 am

    There were many things wrong with your code:

    • if ($this->vardata[$var]) is not the way to check is a variable exists; isset or (if null’s are allowed, array_key_exists) is.
    • The mainclass called a function it doesn’t have. You should have declared it abstract (it would still work otherwise, but you’d be exposing yourself to bugs if you instantiated the mainclass).
    • You didn’t make your mind if you wanted _loadFoo to set a missing array element (work with collateral effects) or return the default value for the foo property. Since you’re returning by reference in __get, I suppose you want to have an actual variable and not just a return value, so I went for the first route. You had _loadFoo doing both things – set the missing array index and returning the value; to complicate things you did not set the array index directly, you relied on overloads.

    Corrected version:

    <?php
    
    abstract class mainclass {
    
        var $vardata = array();
    
        function &__get ($var) {
            if (isset($this->vardata[$var])) return $this->vardata[$var];
            if ($var == 'foo') {
                $this->_loadFoo(); /* called for collaterals */
                return $this->foo;
            }
            return NULL;
        }
    
        abstract function _loadFoo();
    
        function __set ($var, $val) {
            $this->vardata[$var] = $val;
        }
    
        function __unset($var) {
            unset($this->vardata[$var]);
        }
    
    }
    
    class extender extends mainclass {
    
        function __construct() {
    
            var_dump($this->foo);
            $this->_loadFoo();
            echo '<br>';
            var_dump($this->foo);
    
        }
    
        function _loadFoo () {
    
            unset($this->foo);
            $this->foo = array();
    
            $this->foo[] = 'apples';
            $this->foo[] = 'oranges';
            $this->foo[] = 'pears';
    
        }
    
    }
    
    $test = new extender;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.