Programming Tutorials and Developer Forums |
PHP 5 Tutorials – Static Data Members and Methods Posted: 12 Feb 2008 01:47 AM PST In this tutorial you will learn all about static data members and methods
Meaning of static data membersA data member that is commonly available to all objects of a class is called a static member. Unlike regular data members, static members share the memory space between all objects of the same class. Meaning of static methodsA static method is a class method that can be called without creating an instance of a class. Such methods are useful when creating utility classes.
Defining static data members in PHP5To define a static member in PHP5 you need to prefix the class member name with the keyword ‘static’. Look at the example below. class Customer { private $first_name; // regular member static public $instance_count; //static data member }
Defining static methods in PHP5To define a static data methods in PHP5 you need to prefix the class method name with the keyword ‘static’. Look at the example below. class Customer { public function getFirstName() { //body of method } static public function getInstanceCount() { //body of method } }
Accessing static data members in PHP5A static member data can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don’t need to create an instance of that class Look at the example below: class Customer { static public $instance_count = 0; //static data member public function __construct() { Customer::$instance_count++; } public function __destruct() { Customer::$instance_count--; } public function getFirstName() { //body of method } static public function getInstanceCount() { //body of method } } $c1 = new Customer(); $c2 = new Customer(); echo Customer::$instance_count; Output:
Accessing static method in PHP5A static method can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don’t need to create an instance of that class. However, you can also access it with an instance variable. Look at the example below: class Customer { static public $instance_count = 0; //static data member public function __construct() { Customer::$instance_count++; } public function __destruct() { Customer::$instance_count--; } public function getFirstName() { //body of method } static public function getInstanceCount() { return Customer::$instance_count; } } $c1 = new Customer(); $c2 = new Customer(); echo Customer::getInstanceCount(); //this is using the scope resolution operator echo $c1->getInstanceCount(); //this is using the instance variable Output: Rules to keep in mind for static methods
Please feel free to write back for any clarification. Subscribe to my newsletter: Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)
|
You are subscribed to email updates from Geek Files To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.