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 3 years ago.
I was reading through some of the Zend framework's hydration source code and came across something I haven't encountered before:
public function hydrate(string $name, ?array $data = null) : string
I've never seen something type hinted like the $data parameter - ?array
I don't know what to search for, so finding documentation on the syntax used there is really difficult. Does anyone know what it is called when you type hint with a question mark like that, and possibly what it does?
Even if you only point me at the right documentation I'd really appreciate any help.
It declares $data as nullable, as described in the docs
Related
This question already has answers here:
Colon after method declaration?
(2 answers)
Closed 4 years ago.
It's almost impossible for me to search for an answer to this as I don't know what this syntax is called.
I have a server running a relatively old version of PHP (5.5.38) and I'm getting parse errors when I run a file that has the following:
public function foo(): array
{
...specifically the : array suffix.
Can anyone tell me what this syntax is called (so I can research further) and which PHP version introduced it?
This was added in PHP 7, they're called return type declarations.
http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass
This question already has answers here:
Whats the difference between {$var} and $var?
(5 answers)
Closed 5 years ago.
I am a newbie and try to learn PHP especially INSERT INTO statement. I'm know that if we insert value for String we must use '' syntax. But i don't understand meaning of {} syntax for "title" and"link". Any guys can explain for me. Thanks a lot.
On Sql side there is no difference between '$title' or '{$title}' . but in php {$title} is a more complete syntax of $title, that allows one to use :
"this is post {$title}s"
"{$object->data}"
"{$array['data']}"
"{$array['data']->obj->plop['test']}"
The curly braces "escape" the PHP variable and are not passed to MySQL. With a simple variable like $title it doesn't make a difference but with something like $post['title'] it does. for more information check this
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Declaration of Methods should be Compatible with Parent Methods in PHP
I just installed php 5.4.4. and I all of a sudden get a strict warning.
Does someone know what it is?
Strict (2048): Declaration of User::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/User.php, line 3]
In APP/Model/User.php, change the declaration to match the class it extends, Model
function beforeSave( array $options ){
...
If you look closely, you'll notice that the methods signature differs. Model::beforeSave() accepts an optional array, whilst your method doesn't accept anything.
However, the message is not severe. It's a strict, meaning that you're breaking standards, but is not like you're on Titanic.