Static vars - what are they and when should they be used? [duplicate] - php

This question already has answers here:
Static methods in PHP: what for?
(2 answers)
Closed 8 years ago.
I'm trying to figure out what static vars are.
They can be access without instantiating the class but what other benefits do they have and when should they be used?
For example, my class has a private var which holds the name of the twitter feed i'm trying to get.
Should this be static? It never needs to change.

Generally things which aren't instance specific but needs to be stored in a variable should be static variables. Otherwise this manual tells the details: http://php.net/manual/en/language.variables.scope.php
Otherwise you can consider using constants also. For the example you mentioned (as others wrote) using constants seems to be the most sensible. (Either a class constant, or simple one.)

Static variables are for when you want a variable inside a function to keep it's value if the function is called again.
An example of a static variable could be the following.
function addOne(){
static $i = 0;
$i++;
return $i;
}
echo addOne();
echo addOne();
echo addOne();
Which would return
123
Without the static keyword, this would simply return
111
In your question, you mention you have data that won't need to be changed. As the comments in the question state, you should make this a Constant.

In short, static variables can be used for constants.
For example, a Math class can have static variables; PI etc.

Let's say you have something in a class that you need later.
Now, you need that thing but you don't actually need|want|should create a new instance of that class.
That's why you use a static method/property

Related

Why is {'property'} used in PHP [duplicate]

This question already has answers here:
How and why use curly braces: return $this->{$this->action}();
(2 answers)
Closed 6 years ago.
Feel free to re-title this Question because I do not know the proper name for doing this. More to the point, I have seen people using {'property'} when accessing a property inside an object so I set-up an example to try understand however, the property is accessible when I use it and when I don't?
class Example {
public $name;
}
$e = new Example();
$e->{'name'} = 'Kdot';
echo $e->name; // output: Kdot
I have tried changing the scopes and accessing it through a class method but it works both ways, again.
Can someone help me understand what the meaning of using the {} delimiters are? Because from my knowledge, if you stored the parameter inside another variable, this would also work:
$property = 'name';
echo $e->$property; // output: Kdot
They're mostly both just different ways to do the same thing, but with slightly different capabilities.
One difference is that if you want to concatenate inline and use that for a property name, you need the braces:
// example:
$property = 'foo';
echo $e->{$property . 'Suffix'}; // good, uses $e->fooSuffix
echo $e->$property . 'Suffix' // bad, uses $e->foo and adds "Suffix" literal
As well as that, directly access property names need to conform to certain rules. For example if you have a dash in your property name you need to use braces to access it.

'&' sign before function name in PHP [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.

Should I declare global variables at the top of a PHP file? [duplicate]

This question already has answers here:
Are global variables in PHP considered bad practice? If so, why? [duplicate]
(6 answers)
Closed 9 years ago.
I'm using Netbeans IDE, it shows a warning next to undeclared variables - very useful.
If I have this at the top of a file
global $CFG;
then the warnings go away because the variable has been declared.
But is this good practice? Are they any advantages? The code still works without the declaration.
Note: This is for files that have code outside of functions.
it is good practice to declare variables before using them. Declaring them as global within the global scope is superfluous though. You could just do
Instead of doing
global $CFG;
you can just do
$CFG;
The only time declaring them with the global prefix is "useful" is when you do it inside a function to access a globally scoped variable from within the function - but this is usually bad practice, very few cases where this is absolutely necessary.
No, if you wish to use a variable outside of its scope (for example inside a function) you may pass it or globalize it inside the function
function xoxo(){
global $var;
}
There are two things which would make me wary of "declaring" variables in this way.
Firstly, large amounts of code outside of any function probably mean that your code needs re-factoring. At the top level of your code, you might have a handful of lines which call the main actions of the script or page, but saying your declarations would go "at the top of the file" suggests there is rather more than this.
Secondly, "declaring" a variable in PHP is usually synonymous with giving it an initial value of some kind. For instance, setting $params = array() before building up a list of template parameters such as $params['foo'] = get_foo(). Such initialisation should always be kept close to the code using it, so that if you do later re-factor it, the code goes with it.
An example of why always initialising a variable is a good idea is if you end up putting an entire block of code inside a loop of some sort. In the case above, if I was rendering multiple templates and forgot to initialise $params, $params['foo'] could end up being passed to every single template.

What does :: mean in php? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the “::” notation in php used for?
I noticed this code while modifying a friends code and noticed this piece of code: TestPages::LoadMenu();
what does :: mean in php?
A great answer would mean a lot.
Thanks!
It's the 'Scope Resolution Operator'.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in
simpler terms, the double colon, is a token that allows access to
static, constant, and overridden properties or methods of a class.
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php
In layman terms it is used to call static Methods of a Class.
In your example, LoadMenu() is a static function of the TestPages class.
This means that you do not have to create an instance of a TestPages to call LoadMenu()
It is used to access static methods of class, static variables and constants
Read more
It means static class member access, in this case static method invocation.
It's used to access class methods / properties:
http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

create class directly after a property field? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
declare property as object?
in java you can create an object directly after the property field like this:
but it seems not working for php:
class Test {
public $object = new Object();
}
you have to create it in the __construct() and assign it to the property?
thanks
From php.net
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
So no, you cant initialize it to an object. You'll have to do it in the constructor like you said

Categories