This type-hinting only works for validating function arguments, return values, properties, and class constants; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when blah is called, but $bur could be reassigned to a non-Bur value inside the function.
Type-hinting works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.
The type can also be made nullable by adding ? before it. This is identical to a type union with null.
foo(?string $var) { }
// is the same as
foo(string|null $var) { }
You can also use composite types such as string|int as of PHP 8.0.
An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.
Answer from JW. on Stack OverflowDeclaring Variable Types in PHP? - Stack Overflow
What is the proper way to declare variables in php? - Stack Overflow
Variable variables in PHP - What is their purpose? - Stack Overflow
What is the use case for variable variables?
Videos
This type-hinting only works for validating function arguments, return values, properties, and class constants; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when blah is called, but $bur could be reassigned to a non-Bur value inside the function.
Type-hinting works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.
The type can also be made nullable by adding ? before it. This is identical to a type union with null.
foo(?string $var) { }
// is the same as
foo(string|null $var) { }
You can also use composite types such as string|int as of PHP 8.0.
An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.
- Specifying a data type for a function parameter will cause PHP to throw a catchable fatal error if you pass a value which is not of that type. Please note though, you can only specify types for classes, and not primitives such as strings or integers.
- Most IDE's can infer a data type from a PHPDoc style comment if one is provided. e.g.
/**
* @var string
*/
public $variable = "Blah";
As of PHP 7 (which is several years old at this point), primitive types can also be declared for function arguments. Nullability can also be indicated with a ? in front of the type from 7.1 onward. You can declare return types now, too. So this is valid PHP these days:
public function hasFoo(?int $numFoos) :bool {
PhpStorm (my current preferred IDE) is happy to use all of these types for code completion, so I don't need as many phpDoc comments for typing as I used to.
How you are declaring is perfectly alright and proper way.
$test = "";
or
$test = null;
these both are proper ways for declaring empty variables. for more info please visit http://php.net/manual/en/language.types.null.php
You need to declare variables before echoing them out. An example is here:
<?php
$var = "test";
echo $var; // it will echo out test
?>
And trying to echo out a variable this way will generate an error:
<?php
echo $var; // it will generate error
$var = "test";
?>
In addition, you can declare variables in another file and can include that file to echo out the variable somewhere. Remember to include the file first and then call it.
Example vars.php:
<?php
// define vars
$var1 = "Test 1";
$var2 = "Test 2";
?>
Now in another file, include vars.php first and then call the variable:
<?php
require_once"vars.php";
echo $var1;
?>
Sometimes we need software that is extremely flexible and that we can parametrize. You have to prepare the whole thing, of course, but part of it just comes from user input, and we have no time to change the software just because the user needs a new input.
With variable variables and variable functions you can solve problems that would be much harder to solve without them.
Quick Example:
Without variable variables:
$comment = new stdClass(); // Create an object
$comment->name = sanitize_value($array['name']);
$comment->email = sanitize_values($array['email']);
$comment->url = sanitize_values($array['url']);
$comment->comment_text = sanitize_values($array['comment_text']);
With variable variables
$comment = new stdClass(); // Create a new object
foreach( $array as $key=>$val )
{
$comment->$key = sanitize_values($val);
}
This has some uses when referencing variables within classes, and there are some cases where these can actually be required (such as in __get(), __set(), __isset(), and __unset()). However, in most cases it is unwise to use them on global variables.
Be aware that you should NEVER directly accept end-user input when it comes to variable variables. Instead a wrapper function should be used to ensure that only specific inputs are allowed.
In most cases, variable variables are not required, and it is recommended that you avoid them when possible.
I'm learning PHP and with every subject try to use every new concept and I struggled with few, but v.v. just take the cake. Seems to me to be really not that useful? And for some really less keys to type I introduce variables out of nowhere that later I will have to think about where they come from, whereas without them I'd have like an associative array with hint to the purpose of the array and its variable. ( varriable_of_x['some_value']. ) Any ideas?