Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting entire PHP Class

Tags:

oop

php

class foo{
    ....
}

Say that class exists in my code, then later on I no longer need this class and wish to remove it (so I can replace it with a new class later)

Is it possible to delete an entire class from run time?

like image 236
Ryan Copley Avatar asked Jul 18 '12 05:07

Ryan Copley


People also ask

How to remove a file PHP?

To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.

How to do delete function in PHP?

There is no delete() function in PHP. If you need to delete a file, look at the unlink() function.

Is delete in PHP?

There is no delete keyword or function in the PHP language. If you arrived at this page seeking to delete a file, try unlink(). To delete a variable from the local scope, check out unset().


1 Answers

Use unset($classVariableName) to delete the instance and it will free the memory.

If the definition of the class shouldn't be there at run-time, then the class needs to be added in separate file and the file should be included only when it is require. So that you can have two different class definition with same class name. But having two different definition with same name is not a good way to go.

like image 190
Nish Avatar answered Oct 24 '22 06:10

Nish