Re: Scope of Variables and use of global and this->var

From: Date: Tue, 14 Jul 2009 13:01:10 +0000
Subject: Re: Scope of Variables and use of global and this->var
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-295362@lists.php.net to get a copy of this message
> Oh and if one class uses methods in another class .... do I instansiate a > new object of the other class .... I have seen use of OtherClass::Method > ....  is this better method of $obj = new OtherClass()  use The :: is used to access static methods of a class. Static methods can be used without creating an instance of the class because they dont use any of the classes member variables. For example say you have a class with a function for calculating the area of a rectangle: class SomeMathFunctions { public function calculateRectangle($width, $height) { return $width*$height; } } To use this function you would need to first create an instance of the class then call the method using the normal -> : $funcs = new SomeMathFunctions(); $area = $funcs->calculateRectange(10,15); But if you create the function as static by using " public static function calculateRectangle($width, $height) { " then you can access the method by using just 1 call: $area = SomeMathFunctions::calculateRectange(10,15); So for creating utility functions its better to use static methods since you dont get the overhead of creating a new instance of the class.

« previous php.general (#295362) next »