static keyword before variable in a method [duplicate] - php

This question already has answers here:
Static variables in PHP
(5 answers)
Closed 11 months ago.
I found the following method and I don't know what the static keyword does at that point. The variable is initialized with [] so it's empty anytime the method is called anyways.
What does the static keyword do at this point?
public function getSomething($entity)
{
static $collection = [];
if (!$collection[$entity->getPrimaryKey()]) {
$collection[$entity->getPrimaryKey()] = 'something';
}
return $this->doCollection($collection);
}

declaring properties or methods as static allows you to access them without needing to instantiate the class.

Related

Calling static functions with $this [duplicate]

This question already has answers here:
PHPUnit - Use $this or self for static methods?
(3 answers)
When should I use 'self' over '$this'?
(23 answers)
Closed 3 years ago.
I was messing around and discovered that you could actually call static methods with $this->method()
And it got me a bit confused and curious about the differences between the 3 ways (that I know of) to call static methods
$this->method();
static::method();
self::method();
Now, I think I understand the difference between the latter two, but what about the first one?
It is important to understand the behavior of static properties in the context of class inheritance:
Static properties defined in both parent and child classes will hold DISTINCT values for each class. Proper use of self:: vs. static:: are crucial inside of child methods to reference the intended static property.
Static properties defined ONLY in the parent class will share a COMMON value.
declare(strict_types=1);
class staticparent {
static $parent_only;
static $both_distinct;
function __construct() {
static::$parent_only = 'fromparent';
static::$both_distinct = 'fromparent';
}
}
class staticchild extends staticparent {
static $child_only;
static $both_distinct;
function __construct() {
static::$parent_only = 'fromchild';
static::$both_distinct = 'fromchild';
static::$child_only = 'fromchild';
}
}
$a = new staticparent;
$a = new staticchild;
More on https://www.php.net/manual/en/language.oop5.static.php
self using for static method and variable in class
$this using for non static method and variable
static generally using call child class of static methods or variable
For example late static
For example late static binding

What does the Interface object means in the function parameters [duplicate]

This question already has answers here:
Why is type hinting necessary in PHP?
(6 answers)
Closed 4 years ago.
I am not able to understand what is the meaning of having any model or interface object into the method parameters.
For example,
public function checkRights(CommentInterface $comment)
{
return true;
}
so here what does CommentInterface do? why we are not only passing $comment here? How do you name this kind of thing in programming language?
I am new to object oriented php
Thanks.
This is called as Type Hinting.
Type hinting forces you to only pass objects of a particular type. This prevents you from passing incompatible values, and creates a standard if you're working with a team etc.,
check the following example:
class Profile {
private $setting;
public function __construct(Setting $setting)
{
$this->setting = $setting;
}
}
Because we need to use the $setting object inside the function, we inject/pass/type-hint it as a parameter.

PHP class: Global variable vs Accessbale varibales [duplicate]

This question already has answers here:
What does PHP keyword 'var' do?
(8 answers)
Closed 6 years ago.
I have a doubt what is the difference between below class.
class Test {
var $location;
public function __construct() {
$this->location = 'India';
}
}
class Test {
protected $location;
public function __construct() {
$this->location = 'India';
}
}
Why we use var ? What is the purpose of using var as global here.??
Please clarify me.
The keyword before variable name defines the visibility of the variable. It defines the access rights for the particular variable.
var
When using var, it will be publicly accessible through out your project same as public.
protected
When using protected, variable is only accessible for the classes which extends parent class for particular page only.
Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.
Read More Here

instance object can call static method but not static variable [duplicate]

This question already has answers here:
PHP: What if I call a static method in non-static way
(3 answers)
Closed 6 years ago.
I am trying to wrap my head around the static keyword in PHP. Here I wrote a small piece of code where my person1 instance of Human can call a static method called sayRealname(). But when I try to call a public static variable it gives an error. What is the reason for that?
class Human{
public static $age=34;
public static $name='humpty dumpty';
protected static $realname='al';
public static function sayRealname(){
echo self::$age;
}
}
$person1=new Human();
echo $person1->name; // error
$person1->sayRealname(); // prints 34
When you're accessing static content, you need to use the static operator ::.
On the other hand, the arrow operator -> is meant to be used in an instance context, which belongs to the particular instance upon it's being called.
You can read more about this in this part of the documentation.

PHP: static variable outside of class [duplicate]

This question already has answers here:
`static` keyword inside function?
(7 answers)
Closed 9 years ago.
I have seen in 3rd party code a variable declared as static, but outside of any class, simply in a "normal" function.
<?php
function doStuff(){
static $something = null;
}
?>
I have never seen static used this way, and I cannot find anything it in the PHP documentation.
Is this legal PHP code? Is this effectively the same as a global variable? If not, what is the purpose?
From the manual:
Another important feature of variable scoping is the static variable.
A static variable exists only in a local function scope, but it does
not lose its value when program execution leaves this scope.
<?php
function test()
{
static $a = 0;
echo $a;
$a++;
}
?>
Now, $a is initialized only in first call of function and every time
the test() function is called it will print the value of $a and
increment it.

Categories