Home arrow PHP arrow Page 3 - Using Inheritance, Polymorphism and Serialization with PHP Classes

Class Functions Without Instances - PHP

If you are working with classes in PHP, you will sooner or later encounter inheritance, polymorphism, and serialization. The ability to use these three will help speed up your code writing. This article covers how to use them, and more.

TABLE OF CONTENTS:
  1. Using Inheritance, Polymorphism and Serialization with PHP Classes
  2. Polymorphism
  3. Class Functions Without Instances
  4. Serializing
By: Jacques Noah
Rating: starstarstarstarstar / 19
August 15, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

There is a way to use a class function without having to create an instance of a class. This capability has been added since PHP 4 and is useful if you only need a function from a class, but not the entire class itself. The syntax for doing this is:

the_Classname::the_Method();

To demonstrate, lets use the "say_hello()" function in our human class:

<?
class human{
function human($hcolor){
       $this->hcolor=$hcolor;
}
function say_hello(){
echo "Hello!<br>";
}
}
echo "The result of the uninstantiated class function is: <br>";
echo herehuman::say_hello();
?>

As you can see from the code above, all I did was add the human::say_hello() line of code. The say_hello() function is a function of the human class. Normally, to use this function we would have to create an instance of the class, but in this case we did not, so let’s run the code and see what happens:

The above screen shot shows the results of a class function used without instantiating the class.

Remember to include the class from which you want to use the method. Also, as a matter of good coding practice, you can make sure the specific function exists in a class before using it. You do this like so:

if(method_exist(the_classname::function_name())){
//do something here…
}

To check that the say_hello() function exists in our human class:

if(method_exist(human::say_hello())){
//do something here..
}

Destroying Objects

In most object oriented languages there is a built-in capability to free or destroy objects that you no longer want to use. Why do you need to destroy an object? Well, the reason is quite simple. The creation and use of an object is resource intensive, and it is therefore important to free up the resources that get tied up when using objects. This is done by calling the unset() function after you’ve instantiated the object:

$object = new Classname
unset($object);

Remember to call this function at the very end of a script to avoid destroying the object while it is needed by the program.



 
 
>>> More PHP Articles          >>> More By Jacques Noah
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: