Page 1 of 1
Static Variables in php OOP
Posted: Wed Oct 07, 2009 10:12 am
by finchamgroves
Am trying to pick up OOP in a hurry. Mistake!
Expected to access static variables via class::$variable = "" but it doesn't seem to wotk in the code below.
Any help with what I've got wrong?
It works OK if I declare and initialise the variable in the "normal way" rather than using the scope operator:: on the following lin the way I thought I was supposed to!
Any advice much appreciated - Thanks John
<?php
class Philospher
{
//Declaring Static Variable
public static $Static_Variable;
Philosopher::$Static_Variable="The answer to life the universe and everything is 42"
//Set Static Variable
}
//Access from anywhere (if public)
echo Philospher :: $Static_Variable;
?>
Re: Static Variables in php OOP
Posted: Wed Oct 07, 2009 10:32 am
by jackpf
That's not really how you assign properties a default value...I believe it should be more like this:
Code: Select all
<?php
class Philospher
{
//Declaring Static Variable
public static $Static_Variable = "The answer to life the universe and everything is 42";
}
//Access from anywhere (if public)
echo Philospher::$Static_Variable;
?>
Re: Static Variables in php OOP
Posted: Wed Oct 07, 2009 10:47 am
by JNettles
Cases like this its almost better to just go ahead and define a public static function inside of your class that returns the value - because you know that at some point you're going to need to perform some operation on your data or have conditions. Its much cleaner looking code too.
Re: Static Variables in php OOP
Posted: Wed Oct 07, 2009 1:45 pm
by finchamgroves
Thanks guys. Agree that that makes it work OK. The trouble is I'm not at all sure about the rules of the game in terms of when to use the scope operator (double colon), when to use a normal $variable="y" , when to use $this -> , when to use self etc!
Are their any good php OOP tutorials out there starting from basics because I haven't found them yet?
John
Re: Static Variables in php OOP
Posted: Wed Oct 07, 2009 2:06 pm
by JNettles
Do a search for PHP OOP tutorial and you'll find several good results - I'm sure you'll find a site that will walk you through in a way you'll understand.
For now just remember these basics: if you're going to be doing certain operations over-and-over again (for example handling users) you don't want to have just random variables representing the user state. The "User" needs to be a model that is consistently implemented throughout your application. For example, you could define a user visiting your site like this.....
Code: Select all
<?php
$username = "James";
$password = "jargon";
$email = "james@jargonlovers.com";
$authlevel = 4;
This will work, certainly, but you miss out on such a powerful range of operations that PHP provides for handling
objects. Don't think of a User as several different entities floating around on your page - think of it as a
single entity with several different properties attached to it. Your user is an object in your application. So instead of just randomly declaring variables (with little to no naming conventions) we have something that looks like this.....
Code: Select all
<?php
class User
{
public $username;
public $password;
public $email;
public $authlevel;
}
//create a new user
$current_user = new User;
$current_user->username = "James";
$current_user->password = "jargon";
$current_user->email = "james@jargonlovers.com";
$current_user->authlevel = 4;
echo $current_user->username;
You now have a consistent, single container for your User, $current_user. For now, you'll want to stay away from static functions until you have a strong understanding of how classes work. Learn the rules of classes, their properties and their methods.
Before long, the only variables you'll ever need to declare are temporary placeholders and controllers for operations. If you were to take a look at some of my applications, I'd say at least 99% of the variables you see are actually object properties. They are absolutely essential for any sort of medium to large-scale work.
Re: Static Variables in php OOP
Posted: Wed Oct 07, 2009 2:53 pm
by finchamgroves
OK - Think I've got my head around the advantages and the basics if classes, properties and methods.
Will adopt a more patient approach!
Thanks for the advice.