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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:02:49+00:00 2026-05-15T09:02:49+00:00

If I have a class (like below) and in a function I set a

  • 0

If I have a class (like below) and in a function I set a variable (below:$this->example) but I havent declared the variable at the top of the class (e.g var $example;) where and how can I use this variable? I tried using it straight away in another function but it didn’t work (I guess I could have made a mistake but it worked after I declared it in the top of the class)

I have seen this in Symfony for setting variables that you can use in the view and also I came accross it in Phorms to name a couple of examples.

Sorry if this is obvious, I would just like to understand how I can use these variables, including getting the name of the variable(e.g $this->example, by name I mean “example”).

class ExampleClass{
  var $another_var;
  function __construct($data){
    $this->example = $data;
    $this->another_var = $data;
  }

  function exampleFunction(){
    $test = $this->example; //this doesnt work
    $another_test = $this->another_var; //this obviously does
  }
}

Any help would be much appreciated

Regards

Luke

EDIT: (from my reply to DrColossus)

I want to be able to set any variable name in a function and in another function grab any variables set with there name.For example in Symfony I can set $this->completly_random_name = $x in an action class function, then in the view I can use $completly_random_name. There is no way that symfony has set every possible combination of variable names in the top of the parent class.

  • 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-15T09:02:50+00:00Added an answer on May 15, 2026 at 9:02 am

    Skipping the talk of how you normally shouldn’t do this, you can accomplish what you want using PHP’s magic __get and __set functions.

    class MyClass {
      // add one member var that holds all the data
      private $myMagicData = array();
    
      private $another_var;
    
      function __construct($data){
        $this->example = $data; // this will auto-call the __set function
        $this->another_var = $data; // this won't, since $this->another_var was declared above
      }
    
      function __get($name) {
        return array_key_exists($name, $this->myMagicData) ? $this->myMagicData[$name] : null;
      }
    
      function __set($name, $value) {
        $this->myMagicData[$name] = $value;
      }
    
      function exampleFunction(){
        $test = $this->example; // this will auto-call the __get function
        $another_test = $this->another_var; // this won't
      }
    }
    

    Documentation on these and other magic functions.


    Why you shouldn’t do this.

    In my opinion, using the magic __get and __set functions promotes poor programming practice. Let me demonstrate using a famous example: If a glass is half-filled, is the glass half-full or half-empty? The correct answer from a programmer’s point of view is that the glass is too large. What I mean by this is, when you add the magic functions as demonstrated above, you can just keep on assigning variables and it won’t care, but are they necessary?

    Over time, your code will change and you might no longer need old variables that were previously assigned. Normally, you would just remove the variable declaration, meaning your class will now consume less (unneeded) memory. If you forgot to remove one of the old assignments, you’ll find out soon enough. With the magic function functionality, how are you going to keep track of which variables you need, and which you don’t?

    Remember that code should be written primarily for humans to read, and only secondarily for machines to execute. If a second person were to join you and he wonders what variables he has access to in the view, he would either have to go through the code assigning the variables, or print_r($this->myMagicData), rather than just looking at the section of the class where the variables are declared.

    And, of course, there is also the overhead of the magic functions getting called, which may or may not be a concern depending on the situation.

    So, to summarize, manually declaring the variables you need helps:

    1. Keep track of what data you are and aren’t using
    2. Makes your code easier to read for both you and others
    3. Performs faster

    Hope this helps!

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

Sidebar

Ask A Question

Stats

  • Questions 459k
  • Answers 459k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer var pipe_delimited_string = string_array.join("|"); Array.join is a native Array method… May 15, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer How about this: Select the root in the Groups &… May 15, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer public static List<Menu> GetTabListForMenu(string langue) { Page_dbEntities entity = new… May 15, 2026 at 11:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.