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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:19:32+00:00 2026-05-23T07:19:32+00:00

I have a set of variables that i need for multiple other classes. I

  • 0

I have a set of variables that i need for multiple other classes. I extended a ‘nice’ getter function (that guesses for the var name and produces ‘set’/’get’ functions on the fly) to work with setters to.

Example:

In interface/parent/whatever: public $name;

In some other class that loads the ‘mySetterGetter’ class: $set_get = new mySetterGetter(); $set_get->get_name();.

Sadly I can’t use variables in an interface and can’t extend a class with multiple parent classes. Is there some other way to load these “interfaces”/extend the Set/Get class?

What I need to do is the following:

// This should be my "interface" one
class myIntegerInterface
{
public $one;
public $two;
public $three;
}
// This should be my "interface" two
class myStringInterface
{
public $foo;
public $bar;
public $whatever;
}

// This is my setter/getter class/function, that needs to extend/implement different variable classes
class mySetterGetter implements myIntegerInterface, myStringInterface
{
/**
 * Magic getter/setter method
 * Guesses for a class variable & calls/fills it or throws an exception.
 * Note: Already defined methods override this method.
 * 
 * Original @author Miles Keaton <mileskeaton@gmail.com> 
 * on {@link http://www.php.net/manual/de/language.oop5.overloading.php#48440}
 * The function was extended to also allow 'set' tasks/calls.
 * 
 * @param (string) $val | Name of the property
 * @param unknown_type $x | arguments the function can take
 */
function __call( $val, $x )
{
    $_get = false;

    // See if we're calling a getter method & try to guess the variable requested
    if( substr( $val, 0, 4 ) == 'get_' )
    {
        $_get = true;
        $varname = substr( $val, 4 );
    }
    elseif( substr( $val, 0, 3 ) == 'get' )
    {
        $_get = true;
        $varname = substr( $val, 3 );
    }

    // See if we're calling a setter method & try to guess the variable requested
    if( substr( $val, 0, 4 ) == 'set_' )
    {
        $varname = substr( $val, 4 );
    }
    elseif( substr( $val, 0, 3 ) == 'set' )
    {
        $varname = substr( $val, 3 );
    }

    if ( ! isset( $varname ) )
        return new Exception( "The method {$val} doesn't exist" );

    // Now see if that variable exists:
    foreach( $this as $class_var => $class_var_value )
    {
        if ( strtolower( $class_var ) == strtolower( $varname ) )
        {
            // GET
            if ( $_get )
            {
                return $this->class_var_value;
            }
            // SET
            else 
            {
                return $this->class_var_value = $x;
            }
        }
    }

    return false;
}
}
  • 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-23T07:19:32+00:00Added an answer on May 23, 2026 at 7:19 am

    It sounds like you want something like this:

    // An abstract class that can't be instantiated but which provides a __call method to other classes that extend this one.
    abstract class mySetterGetter
    {
      function __call($val, $x)
      {
        $_get = false;
    
        // See if we're calling a getter method & try to guess the variable requested
        if( substr($val, 0, 4) == 'get_' )
        {
          $_get = true;
          $varname = substr($val, 4);
        }
        elseif( substr($val, 0, 3) == 'get' )
        {
          $_get = true;
          $varname = substr($val, 3);
        }
    
        // See if we're calling a setter method & try to guess the variable requested
        if( substr($val, 0, 4) == 'set_' )
          $varname = substr($val, 4);
        elseif( substr($val, 0, 3) == 'set' )
          $varname = substr($val, 3);
    
        if ( ! isset($varname) )
          throw new Exception("The method {$val} doesn't exist");
    
        // Now see if that variable exists:
        foreach( $this as $class_var => $class_var_value )
        {
          if ( strtolower($class_var) == strtolower($varname) )
          {
            // GET
            if ( $_get )
              return $this->class_var_value;
            // SET
            else
            {
              $this->class_var_value = $x[0];
    
              return;
            }
          }
        }
    
        return false;
      }
    }
    
    // myString
    class myString extends mySetterGetter
    {
      public $foo;
      public $bar;
      public $whatever;
    }
    
    // myInteger
    class myInteger extends mySetterGetter
    {
      public $one;
      public $two;
      public $three;
    }
    

    You can also “fake” inheriting by multiple classes as in this previous Stack Overflow question: Can I extend a class using more than 1 class in PHP?.

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

Sidebar

Related Questions

I have a set of numbers that I need to pass from a function
I'm having trouble with global variables in php. I have a $screen var set
I currently have my PHP class variables set up like this: class someThing {
I have set up a Django application that uses images. I think I have
I need to have multiple submit buttons. I have a form which creates an
I have a string that stores some variables that must be executed to produce
I have a legacy C Linux application that I need to reuse . This
I'm trying to use python to set environment variables that will persist in Pythons
I have a set of coupled equations for the variables H , W ,
I have c++ code that has grown exponential. I have a number of variables

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.