-1

I have two class like this:

class one
{
    public $var1 = 'anythig';
}

class two
{
    public $var2 = 'anythig';
}

I want to know when I create a object instance of these classes what happens? My point is about the values stored in the memory. In reality I have some big class, and my resources are limited. then I want to know, If I put NULL into my class when don't need to it anymore is good ? and help to optimizing ?

I have a switch() to include the desired class. something like this:

switch{
          case "one":
          require_once('classes/one.php');
          break;

          case "two":
          require_once('classes/two.php');
          break;
      }

Every time I only need one class. When I define a new object ($obj = new class) what happens to my class previously defined as object instance? that is remain in memory? and if I put NULL is helpful ? Please guide me ..

Edit:

The last line is useful or not ?

$obj = new myvlass;
echo $obj->property; // there is where that my class is done
$obj=NULL;
  • Either assigning `null` or a new instance to an existing object variable will destroy the previous object, assuming nothing else references it. – Ja͢ck Jun 30 '15 at 02:38
  • @Ja͢ck what ? when I create a instance of class1 like this `$class1 = new class1;` and then create a new object of class2 like this `$class2 = new class2`, now `$class1` will be destroy ? –  Jun 30 '15 at 02:44
  • @Ja͢ck so? what should I do ?! I put `NULL` in them ? how can i release the memory ? –  Jun 30 '15 at 02:49
  • `unset($class1, $class2);` destroys both objects. – Ja͢ck Jun 30 '15 at 03:03
  • @Ja͢ck aha, please check out my **Edit** and just tell me yes or not, tnx –  Jun 30 '15 at 03:05

2 Answers2

0

What determines when a class object is destroyed in PHP?

The PHP manual states that "the destructor method will be called as soon as all references to a particular object are removed" which is true (although can lead to some undesirable behaviour.)

It wouldn't really matter if you explicitly set an object variable to be NULL, PHP would destruct it anyway.

Community
  • 1
  • 1
Verox
  • 360
  • 1
  • 12
  • look, my classes are very heavy, and my online user are very much, and my resource are limited, still you tell *It wouldn't really matter if you explicity set an object variable to be `NULL` ? what is your opinion about `unset($obj); ` ? thanks –  Jun 30 '15 at 02:14
  • unset($obj); does the same thing as setting it to null. It wouldn't really matter that much because PHP will do the same thing (destruct the class) regardless if you set it null or not. – Verox Jun 30 '15 at 02:21
  • aha, then will php do it ?! that's good, just should I do anything ? my point is is there need to I write anything into `__destruct()` ? –  Jun 30 '15 at 02:23
  • Closing file handles, database connections, stuff like that. – Verox Jun 30 '15 at 02:25
  • Is the code running through a loop in which the switch call will get called multiple times? Once the script ends, it will get destroyed regardless. – Arturo Alvarado Jun 30 '15 at 02:44
0

i would recommend my best way to implement class as follow :

<?php
class scSendMail
{
    protected $from;
    protected $toList;
    protected $replyTo;
    protected $subject;
    protected $message;
    public function __construct()
    {   
        register_shutdown_function(array($this,'__destruct'));
        $this->setFrom("updates@planetonnet.com");
        $this->setReplyTo("noreply@planetonnet.com");
        $this->setSubject("Update from PlanetOnNet.com");
    }

    public function __destruct()
    {
        unset($this->from);
        unset($this->toList);
        unset($this->replyTo);
        unset($this->subject);
        unset($this->message);
    }


    public function sendMail()
    {
     // ..... body
    }


}

?>

in this way whenever object is not needed, it will destruct itself and free ups memory by unsetting variables used.

you can initiate another object anytime to replace with new object but be careful to use methods according to what object currently it is holding.

you can set to NULL to free ups memory whenever you dont need to use it anymore and use new variable to use new object.

Narayan Bhandari
  • 426
  • 3
  • 11
  • Doing all those `unset()` operations are pretty useless, because they would be taken care of by the standard destructors. – Ja͢ck Jun 30 '15 at 02:44