9.8 Using Functions in a Class Without Initializing an Object


You want to use functions in a certain class without initializing an object via the new statement.

Technique

Use the :: notation, which enables you to access functions in a class without using an object:

 <?php class Conversion {     function feet_to_meters($feet) {         return $feet * 0.3054;     } } $average_height = Conversion::feet_to_meters(6); ?> 

Comments

The :: notation enables you to access function in classes without an object, but how do you access variables from within a class without an object? You don't. At the time this book was written, PHP supports only the capability to access methods ”not variables ”without using an object. However, after you have an object, you can access both variables and functions via that object.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net