"Build a Weather App (2015)" was retired on June 6, 2018. You are now viewing the recommended replacement.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Static Methods and Properties

What's the point of static??

In the video example, why not just leave out "static" when you define "$manufacturer" so you can simply use $shirt->manufacturer instead of messing with the "::"? It just seems to complicate things, but I'm sure there's a reason why it exists....

Thanks!

5 Answers

Jason Cullins
PLUS
Jason Cullins
Courses Plus Student 4,893 Points

When using the $shirt->manufacturer for instance, you had to declare a new instance of the object (i.e.: $shirt = new shirt(); ), which means you have a copy that you're working with. Well a copy is just that, a copy, where as static, you're always working with the original. If the original changes, then it will be changed everywhere.

It has it's place. I personally use static objects to define things like path or configuration variables that are pre-defined by the admin.

For instance:

vars.php

public class ConfigVars { public static $url_path = '/var/www/htdocs'; }


Then if i ever need to use that variable, i simple do the following

mypath.php

<?php

include('vars.php');

echo('The path to my web documents is: ' . ConfigVars::$url_path);

?>


So as you can see above, ConfigVars::$url_path is always there waiting for me because i include vars.php in my files.

Now typically what I do is i have a classes.php file where i have included all of my .php files and then i have a globalvars class and maybe a debug class inside of my classes.php, so then i only have to include the classes.php file in all of my other .php files and my globalvars is always available as well as all of my other class includes are done as well.

Hope this helps!

Ezekiel dela Peña
Ezekiel dela Peña
6,231 Points

Oh finally I got it thanks to this example. Yet again the tutorial lacks some certain of information that made it difficult to understand or is it just me. O well thanks for this.

nice, thanks for the practical example.

That helped a lot Jason - much appreciated!!

Jason Cullins
PLUS
Jason Cullins
Courses Plus Student 4,893 Points

Usually you use classes to keep things better organized.

Take this for instance. You may be connecting to 2 different APIs for instance.

Lets say you have API1 and API2. You can set a static variable with the same url variable in both classes and can call them separately.

like API1::$url_path and API2::$url_path ...

Also keep in mind, using classes is the more up to date method of programming being that it's more object oriented way of programming instead of a procedural way of programming.

Also, just because you have a static variable in a class, not all variables or function have to be static...

Using classes also helps you to be able to re-use your code later. So if you did have the API1 and API2, lets say 1 api is for twitter and the other is for Facebook, you'd be able to re-use this code much easier later with other php sites you're using because you know variable and function names aren't going to conflict with other variable names and function that you might be working on..

Hope this helps answer your question.

Keith Clark
Keith Clark
4,074 Points

Just now taking this course, so sorry about the late question. If you are simply storing static variables, and still have to include the vars.php anyway, why would you simply not create an include file that has a simple $url_path = "......" ??

What is the advantage of creating the class, then the static variable?

Sorry if I am missing the obvious here.

Keith

Keith Clark
Keith Clark
4,074 Points

Yes, it does make sense. Thanks.

I guess the biggest problem I keep running into is the mental block of having programmed using PHP in a procedural method (top down) for years and now changing over to using PHO in an OO environment.

Keith