I debut in this community with the following doubt that I do not finish seeing.
If I create a class in which I establish some properties and methods of type static, how can I later retrieve the value of those properties from another file?
Example:
final class website {
public static $valor;
private function __construct() {
}
public static function setValor($x) {
self::$valor= $x;
}
public static function getValor() {
return self::$valor;
}
}
If I try to do this from another .php file the result I get is null.
website::getValor();
Can someone give me an idea?
Thank you
The reason for the error
It returns you
NULL
because when you do:At that time the
$valor
class member has no data. In fact, usually a property declared asstatic
should be declared with some default value (we'll see why later).If you do this for example:
And then:
You will see a
1
, because the class property has acquired a value that it did not have before.Or if you declare it with a default value, you will see that value when you invoke it:
What is a property or member for
static
? When should I declarestatic
?You have to be careful when declaring a member
static
in a class.From the outset it must be said that the use of static members (as well as the declaration of global variables), is considered a bad practice . Although this does not mean that you have to see the devil when we see a static. There are cases in which they could be convenient and we will see it with some practical examples.
The peculiarity of the properties declared
static
in a class is that they belong to the class itself, and not the instance of the class . That means that we can make use of them without having to create a new instance of that class. That's why we can access it just by doing:Clase::$miembroEstatico;
The question then is: when is this justified? That is, in what cases would it be better to have a property of the class that you can access without needing to instantiate that class?
A very common case is when we need a counter in the class for whatever. That property can be declared as
static
.Usually those static properties exist in utility classes which we make use of in many parts of the program.
Let's consider a class
Utils
where for whatever reason we need to, among other things, keep a counter:If in various parts of the program we need to modify/obtain said counter, both a property and a member are justified in that class
static
to handle that situation.Imagine several parts of the program:
The information that will be obtained in each case will be:
Another very common example where a static method would be justified in this case would be that you would need
Utils
a method in your class to format content.This is something that I use in an application in which, for reasons that are not relevant now, I handle certain information saved with symbols that are later replaced by another type of content.
In the database the content is more or less fixed, but certain symbols are added according to variable parameters such as the day of the week, the month we are in, etc.
In that case the class
Utils
could have a static method that we will callformatText
, which will receive the text as it is constructed and make modifications to it by changing the symbols with a pre-established contract.The method is longer and does more complex things with the parameter
$nTiempo
when required.Another interesting and possible candidate
static
method would be a method where you show, for example, the Copyright or the license under which you share some content.It should be said that all this is declared within the same class
Utils
, which, as its name indicates, is a utility class that is very helpful in large programs to have things that we repeat very often in one place. Also, this gives you the ability to change anything that needs to be updated in one place.Another very important aspect: do not confuse the usefulness of a static member with a constant
An error that can be made, especially starting out, is to confuse
static
withconst
. Is not the same. A constant is a value that will never change . A clear example of a constant would be thePI
, which, until proven otherwise, must always be:3.14
.To better understand the difference, you can read this Stackoverflow answer in English , if you don't understand it, you can also ask your questions here in the Spanish-speaking community.
Other things to consider
You have to take into account what @Marcos says in his comment. A getter/setter is not justified if it
$valor
is declaredpublic
, since it could be obtained (get) and modify (set) directly, without going through those methods, as shown above.This usually happens when we don't fully understand in which cases to use a property
static
and when not. I think that with the explanations given it is clearer :)Links:
For more details, you can refer to the following articles: