PHP Unset Class Definition

Web developers use the PHP programming language to quickly deploy dynamic websites such as eCommerce sites and blogs. While designed as a procedural programming language, PHP also enables you to create classes and objects, thus instituting an object-oriented programming paradigm. Because of this, functions in PHP interact with object as well as variables. The unset function, for example, is used to free memory taken by objects no longer in use.

  1. PHP and Procedural Programming

    • Developers conceived PHP originally to represent a procedural programming paradigm. This essentially means that the line of execution in a PHP program moves from line to line. While programming in PHP involves code across multiple files, the execution of a web page including PHP will usually occur straightforwardly, one line at a time. As an interpreted language, this means that variable assignment and deletion also occurs one line at a time.

    Object-Oriented Programming in PHP

    • PHP includes ways to program in the object-oriented paradigm as well. With object-oriented programming, programmers create "objects" that represent data and operations that data takes part in. These objects are reusable, and minimize code and pattern rewriting. Code in object-oriented programs often exists across multiple files, which define multiple objects and their functionality. PHP uses object-oriented programming to help bring the concepts of clean, reusable code to Web development.

    Objects and Memory

    • In object-oriented programming, just as in procedural programming, variables and objects reside in memory once created. For simple variables such as integers or characters, memory use is usually quite small. But for objects, which often contain many types of data as well as other code to define functions that work on that data, memory use across a Web server can become quite large. It becomes desirable for programmers to manage memory manually by deleting objects after they have served their purpose in the program.

    The Unset Function

    • The unset function in PHP frees these values from memory. While PHP contains a garbage collector like other languages such as Java, the programmer might want to manually delete a variable or object. The garbage collector automatically checks the code for unused variables and objects and deletes them. If the programmer wants the object deleted at a certain moment, however, the unset function enables this, as illustrated in the following example:

      <?php
      class A{
      public $b = 0;
      }

      $temp = new A(); //declare object of type A
      unset($temp);

Related Searches:

References

Comments

Related Ads

Featured