Jump to content

Accessing Class Properties & Mehods


jimfog
 Share

Recommended Posts

What is the difference in accessing class properties & methods with the double semicolon(::) with accessing them with them arrow operator-> I miss something here.

  • Like 1
Link to comment
Share on other sites

You can only access methods and properties with the two semi-colons when the method or property are static and not dependent on the instance of the object. For example:

class House {  public static $houses = 0;  function __construct() {    House::houses++;  }}$a = new House();$b = new House();echo House::houses; // displays 2// There are 2 houses: $a and $b

Link to comment
Share on other sites

So the :: (double semicolon) is only for for static properties and methods. Static in other words in this regard means the value $houses will always remain the same when it is altered? So when making a new instance of house(), and it's been altered so many times, the value will always remain the same no matter how many instances you make? Can static properties still be accessed using the -> operator at all? (I know in this case the :: is the right option but I thought I'd ask anyway) Can you give us an example of a static method/function? Thanks Fox.

Link to comment
Share on other sites

No, you can't access static properties with the arrow operator. A class can be used as a simple collection of functions if they're all static. Here's an example simplified and translated to PHP from something I was doing in Java at one point:

class Geometry {	public static $threeQuarterCircle = 3 * M_PI_2;		public static function getTangent($angle) {		if(cos($angle - M_PI_2) >= 0.999999) {	   	 return INF		} else if(cos(angle - Geometry::threeQuarterCircle) >= 0.999999) {	   	 return -INF		} else {			return tan($angle);		}	} 	public static function getTangent2($y, $x) {		if(abs($x) < 0.000001 && $y > 0) {			return INF;		} else if(abs($x) < 0.000001 && $y <= 0) {			return -INF;		} else {			return $y / $x;		}	} 	public static function getXComponent($angle, $modulo) {		return($modulo * cos($angle));	} 	public static function getYComponent($angle, $modulo) {		return($modulo * sin($angle));	}} echo Geometry::getTangent2(100,50);echo Geometry::getYComponent(M_PI * 0.25, 10);

  • Like 1
Link to comment
Share on other sites

Static classes are classes which you only need one of. A class to handle logging events would be static, because you don't need multiple event loggers. The entire application would use the static class to log events. A database class might be another example of a good use for a static class. Utility classes are also generally static, and Javascript has some analogous things (even though Javascript doesn't support classes). The Math object in Javascript can be thought of like a static class. You don't create new instances of the Math object to do things, you just directly use the methods like Math.random or Math.round. In PHP, one of the advantages of static classes are that they are always global. Once you declare the class then you can use it in any function or any other class without saying it's global or without instantiating a new object.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...