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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:31:14+00:00 2026-05-29T22:31:14+00:00

I was unable to find a similar question on Stackoverflow, although I am sure

  • 0

I was unable to find a similar question on Stackoverflow, although I am sure someone has probably asked this before.

I have a class with methods that may be called several times per page. Each time the method is called I need to make sure the public variables are reset to their defaults, UNLESS they have been set before calling the method.

This cannot be achieved using a simple if condition because there is no way to tell whether the value has been set or is still set from the last method call

I cannot think of a way to achieve this because I cannot call my __construct method (which sets all the default values), as this would overwrite the parsed values. However, I need to reset them to prevent values from the last method call from being parsed.

The obvious answer is to give different names to the public variables and the return variables. I will do this if there is no other option but I like to keep the number of variables to a minimum

It is very hard to explain this in writing so I will update this question with an example of what I mean in code.

UPDATE

An example of where a problem may occur:

<?php

class test{
    public $return_array;
    public $return_string;
    public $return_bool;

    function __construct(){

        // Set the default values
        $this->return_array = false;
        $this->return_string = false;
        $this->return_bool = false; 

    }

    public function method(){
        // ... do something
        $array = array('test');
        $string = 'test';
        $bool = true;

        // Only return variables if asked to
        $this->return_array = $this->return_array ? $array : NULL;
        $this->return_string = $this->return_string ? $string : NULL;
        $this->return_bool = $this->return_bool ? $bool : NULL;
        return;
    }
}

// Initiate the class
$test = new test;

// Call the method the first time with one parameter set
$test->return_array = true;
$test->method();

// Print the result
print_r($test->return_array);

// MOST OBVIOUS ANSWER WOULD BE TO RESET VARIABLES HERE LIKE SO
$test->reset(); // HOWEVER, I DO NOT WANT TO HAVE TO CALL THIS EACH TIME I CALL THE METHOD, HERE LIES MY PROBLEM!

// Call the method again with different parameters
$test->return_string = true;
$test->return_bool = true;
$test->method();

// Print the result
echo $test->return_array;
echo $test->return_bool;

/* The problem lies in the second call of the method because $test->return_array has not been reset to its default value. However, there is no way to reset it without affecting the other variables. */

?>

This is basically a very long winded way of asking whether it is possible to reset a classes variables to their default values, whilst ignoring the ones that have been parsed to the method being called

  • 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-29T22:31:15+00:00Added an answer on May 29, 2026 at 10:31 pm

    There are several ways to achieve this but they all bottle down to the same solution. Calling a function after each method that resets the variables within the class. Best way to do this is at the end of each method before the data is returned.

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

Sidebar

Related Questions

This has probably been asked before on SO, but I was unable to find
I feel like this one has been asked before, but I'm unable to find
First off - apologies if this or a similar question has been asked before.
I think it might have been asked before but i was unable to find
Someone asked a similar question before , getting the following error when I run
My question is similar to this question and since I was unable to find
I have been unable to find any decent documentation on this function. The code
This is a simple question yet I was unable to find any information at
I have looked around and was unable to find any questions regarding this exact
I suppose similar questions were already asked, but I was unable to find any.

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.