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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:35:49+00:00 2026-05-28T04:35:49+00:00

Previously a class which I’m now rebuilding had a member variable $settings which was

  • 0

Previously a class which I’m now rebuilding had a member variable $settings which was an array of settings, strangely enough.

class MyClass {
    public $settings = array();

    public function __construct() {
        if( empty( $this->settings ) ) {
            $this->settings = require( 'settings.php' ); // e.g. return array('setting1'=>4);
        }
    }
}

These settings were accessed by $object->settings['keyname'];

The means by which these keys are accessed has been moved into a method now. However, the application itself is riddled with calls to $object->settings['keyname']. I was wondering is there a way which I can catch any calls to the $settings member variable and return it using the new function.

I’ve looked at __get($name) but $name only contains settings rather than the array key which I need. What I’d need to pass would be the keyname to the my $object->get() method.

The reason I want to do this is so that I can trigger errors in a log file showing me where the deprecated calls to $object->settings[] are without breaking the application. Obviously setting $setting to private would give me lots of fatal errors and I could work through but there are multiple developers working on this codebase which I’d prefer not to break. If I could implement this as a temporary solution it’d help.

I realise there are repositories etc which we could use so that I could work on it separately and check it in afterwards but I’m looking for a quick, temporary solution as we’re porting our codebase to Git soonish.

  • 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-28T04:35:50+00:00Added an answer on May 28, 2026 at 4:35 am

    Totally possible:

    <?php
    class Logger {
        public function log( $name, $backtrace ) {
            /**
             * $name is the name of the array index that was called on
             * MyClass->settings( ). $backtrace contains an array in which
             * you can find and determine which file has accessed that index,
             * and on which line.
             */
            $last = array_shift( $backtrace );
            $message = sprintf( "Setting '%s' was called by file '%s' on line %d",
                $name,
                $last['file'],
                $last['line']
            );
    
            echo $message;  
    
        }
    }
    
    class MyClass {
        protected $settings;
    
        public function __construct( ) {
            $this->settings = new Settings( new Logger( ), array( 'foo' => 'bar' ) );
        }
    
        public function __get( $name ) {
            if( $name === 'settings' ) {
                return $this->settings;
            }
        }
    }
    
    class Settings extends ArrayObject {
        protected $logger;
        protected $settings;
        public function __construct( Logger $logger, array $settings ) {
            $this->logger = $logger;
            parent::__construct( $settings );
        }
    
        public function offsetGet( $name ) {
            $backtrace = debug_backtrace( );
            $this->logger->log( $name, $backtrace );
            return parent::offsetGet( $name );
        }
    }
    
    $myClass = new MyClass( );
    echo $myClass->settings['foo'] . "\n";
    

    Sure, it’s hackish, and you probably don’t want to keep this in your codebase, but for logging deprecated uses, it might be extremely helpful. Just log for a set period of time, and then replace the $this->settings = new Settings( ) with $this->settings = array( ).

    By the way, the output of that exact code is the following:

    berry@berry-pc:~/Desktop% php foo.php
    Setting 'foo' was called by file '/home/berry/Desktop/foo.php' on line 53
    bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses CoreData. I previously had a class named Marker
Previously, I had a class that wrapped an internal System.Collections.Generic.List<Item> (where Item is a
I have a custom class called NoteView that extends FrameLayout . Previously, I had
I can not access public method of my class MyPanel which extends JPanel .
I've created a Canvas class which has an Array of multiple instances of CanvasEntity
Previously I've asked about inserting a column into a dataset . I now have
Previously I've been using some older version of django-registration which seems to be deprecated
I mean the one which was previously established as DB = Sequel.sqlite('my_blog.db') or DB
Basically, I have a pure virtual class Base, and a concrete class Derived which
Say I have 4 div elements with class .navlink , which, when clicked, use

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.