What exactly is &$this referring to in PHP? [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
class ClassOne
{
private $foo = null;
public function __construct()
{
$this->foo = new ClassTwo();
}
public function doStuff()
{
$bar = &$this->foo->someVariable;
}
}
I know that $this refers to the current object, and & is a reference symbol for changing the original variable.
What is &$this referring to?

Thanks to #k0pernikus and #trincot for clarifying that & refers to the entire $this->foo->someVariable
So the original author wanted to change someVariable in $this->foo->someVariable. Since it was verbose, he assigned the reference to $bar.
However, this was done in PHP4. There must be a better way to write this in PHP5.

Related

PHP question mark operator (not a ternary operator) [duplicate]

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Closed 4 years ago.
I ran into a code in PHP, which has strange question mark operators. Since the code itself doesn't have any comments on that part, i tried to google it, but failed to succeed.
The code i'm interested in goes like:
<?php
class Cart
{
private $_user;
private $_items = [];
public function __construct(?User $user)
{
$this->_user = $user;
}
public function getUser(): ?User
{
return $this->_user;
}
My first thought was of type hinting, which was introduced in PHP 7, but it's apparently not the case, nor it is the strict type declaration. I've got no idea, what it is. Can you help me?
http://php.net/manual/en/migration71.new-features.php
Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.
This means your parameter or return can be null.
PHP 7.1 introduced it : http://php.net/manual/en/migration71.new-features.php

What does prepending '&' to a function name mean in PHP? [duplicate]

This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.

What does backslash do as prefix to a function name? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
Preparing for the ZEND-Exam, I found a question where a class redefined the strlen-function:
namespace MyFramework\MyString
{
function strlen ($str)
{
return \strlen($str) * 2; // return double string-length
}
}
I never came across that "\function"-thing before and could not get an explanation from my trainer - can somebody pls. help shed some light...?
It calls a function from the global namespace.
You need it only if there's a function of the same name in the current namespace.
Introduced in PHP 5.3, \ is a namespace separator.
In your example, the outer strlen is MyFramework\MyString\strlen, the inner strlen is the global one.
Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.
Reference: http://www.php.net/manual/en/language.namespaces.global.php

finding the name of method i am in [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get current class and method?
How can i find the name of the method i am using in php? I found how to do this in C but not in PHP. I found a Q on here which roughly talked about magic constants (here) but I didn't really get it. In the following example I want $thisMethodName to be 'model_databaseLogin'
EG:
public function model_databaseLogin()
{
$thisMethodName = ... ;
return $this->model_methodCheck( $thisMethodName );
}
Is this possible in php?
You need the "magic constant" __METHOD__. The magic constant docs should be helpful.
So your code would be:
public function model_databaseLogin() {
$thisMethodName = __METHOD__;
return $this->model_methodCheck($thisMethodName);
}
The simplest answer is the magic constants to which you refer; specifically __FUNCTION__
These are called "magic" because their value is actually contextually dynamic.
public function model_databaseLogin()
{
$thisMethodName = __FUNCTION__;
return $this->model_methodCheck( $thisMethodName );
}
There is another way, via debug_backtrace(), but that is decidedly less efficient!

When to add brackets after new class name? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP class instantiation. To use or not to use the parenthesis?
Omission of brackets and parameter-less object constructors
With or without the brackets, the new Class seems not bother. So, I doubt what's the usage of the brackets (). I searched php manual, didn't get it. Could anybody explain?
The purpose of the brackets is for you to enter any arguments that your constructor may accept.
class Example{
private $str;
public function __construct($str){
$this->str = $str;
}
public function output(){
echo $this->str;
}
}
$ex = new Example; // missing argument error
$ex = new Example('Something');
$ex->output(); // echos "Something"
If your class constructor does not accept any arguments, you may leave the brackets out. For good code sake, I always keep the brackets, whether or not the constructor accepts any argument.
Most coders coming from C# or Java background would keep the parenthesis as it is more familar to them.

Categories