How can I check if a method is defined? [duplicate] - php

This question already has answers here:
Check if method exists in the same class
(4 answers)
Closed 3 years ago.
I try to set the value of my variable to "", in the case that getLove is not defined:
`$money = $dollar->getCash()->getLove ?? "";`
But I get still the error message:
Attempted to call an undefined method named "getLove" of class
"Proxies__CG__\App\Entity\Happiness".

Using method_exists, note that this does not take "__call()" in consideration.

Related

How to check if variable is an Enum or a Case? [duplicate]

This question already has answers here:
How to check if enum type?
(2 answers)
Closed 10 months ago.
There is a lot of type checking functions in PHP (is_string, is_resource etc) but there is no function to check if variable is an Enum or a Case. How to check it properly?
You can check that it implements the UnitEnum interface.
if ($variable instanceof UnitEnum) echo "it's an enum.";
According to the PHP manual, this interface exists specifically for type checking.

Why an intialized variable is causing function name must be a string error [duplicate]

This question already has answers here:
Laravel 5 Function name must be a string bug
(2 answers)
PHP Error: Function name must be a string
(8 answers)
Closed 3 years ago.
error for intialize variable
$errors=array();
$errors =[];
If i intialize my variable errors to array() it gives the error, if with [] there is no error can someone explain why?

What's the best way of checking if an object property in php is undefined? [duplicate]

This question already has answers here:
Check if a variable is undefined in PHP
(8 answers)
Closed 4 years ago.
What's the best way of checking if an object property in php is undefined?
You can use is_null
Or
!isset($object)
Example :
I want to check if the input is undefined. So, I can show errors.
if (!isset($_POST['myInput'])) { echo "error"; } else { // do the code }

Ambigous PHP error message [duplicate]

This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 7 years ago.
Having this:
...
private $responseBuffer = array();
...
and within this line:
$lm = end(array_values($this->responseBuffer));
I get
Error: Only variables should be passed by reference (2048)
as both end and array_values are built in and do not have a call-by-referenceI'm puzzled, any one an idea?
(purpose: get the latest value out of $responseBuffer)
end function receives the arguement by reference, do like this:
$var = array_values($this->responseBuffer);
$lm = end($var);

Can I access a PHP Class Constant using a variable for the constant name? [duplicate]

This question already has answers here:
Accessing a class constant using a simple variable which contains the name of the constant
(5 answers)
Closed 10 months ago.
When accessing a class constant I see that I can use a variable for the class name, e.g. $classname::CONST_VALUE.
What if I want to use a variable for the constant name, e.g. self::$constant. This does not seem to work. Is there a workaround?
$variable = $classname.'::'.$constant;
constant($variable);
See the docs: http://php.net/constant

Categories