John Davidson

php - Checking "PropertyNotSetInConstructor" on a custom function with Psalm

0 comments
Message:


Is there a way I could tell Psalm to check other functions than __construct() methods for ensuring all properties are set when a class is instantiated ?


In some PHP library, Psalm complains about PropertyNotSetInConstructor because it is not able to infer the whole work that is done in each constructor. The constructors all rely on calling dynamically the right function using call_user_func_array(). I am not suprised Psalm cannot see that but I need a workaround to show it the right function to analyze as a constructor.


The underlying reason is that we mimic constructors with different number of arguments so we can write both $x = new X() and $x = new X($a, $b).



My Answer:
if a variable is set or not can be done using the `isset()` function in PHP. This function returns true if the variable is set and not null, and false otherwise. Here's an example:

php
$variable = "Hello";

if(isset($variable)){
echo "Variable is set";
} else {
echo "Variable is not set";
}


In this example, the output will be "Variable is set" because the variable `$variable` is set to a value. If the variable is not set, the output will be "Variable is not set".

Rate this post

5 of 5 based on 9908 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us