Avatar of swhitlow
swhitlow asked on

Class __construct destroy all properties

I have a class like this:

class Object extends Object2 {

     public $property1;
     public $property2;

     function __construct() {
          $this->property1 = '';
          $this->property2 = '';
          // more code goes here, etc.

     function do_something(){
          return $this->property1;
I was wondering if there were a way to clear all the properties (there are actually quite a few) in the construct statement?  I know that normally you would not need to do this. However, I need to. What would be the best way to go about this?


Avatar of undefined
Last Comment

8/22/2022 - Mon

I would just set the properties to = '' in the variable declaration.

Like public $property1 = '';


That is what I am doing now. Although, in my code I have a case where I need to do this:


After I had already initialized the class object. So, I am needing to call the construct again and am needing an easy way to wipe out all the properties.

Would you happen to have any other suggestions?


Well, I'm not sure exactly how you're going about it, but I would just create my class as is, and then from another class, re-instantiate the Object. So, if I had a file main.php and in it was

public $theClass;
public $theClass2;

function __construct()

$theClass = new Class();
$theClass2 = new Class();

That would be the proper way to use a class multiple times.   Is this what you are trying to do?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

That is actually what I was trying to avoid. I thought it would be too much additional overhead to instantiate a new object in memory. Am I wrong here?

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

how do you kill an object the correct way? I have heard alot of debate about doing this. Sorry about the questions - just want to make sure and do this right.

Just set your object to NULL. That will do the trick, and it's how many OOP languages handle object destruction.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Ok - I had heard about using the unset() function. Didn't know if that would be correct or not.

So, just doing the $obj = null? That's it?

Yes, but you can use unset(), too.  It does the same thing :)