I'm making a function of my UserHandler.php class and it's called insert :
public function insertar($usuario){
echo gettype($usuario);
}
What I wanted to do before doing the insert is to make sure that the object is of type User .
I have been doing the following test:
$uh = new UsuarioHandler();
$var = new Usuario(
0, "jose", "apellidos", "[email protected]", "password",
"españa", '1434-04-03', '2000-01-01'
);
$uh->insertar($var);
$uh->insertar($var);
What happen? because the function insert() returns me: object
. It's fine, but I want it to return, for example, a string of the name of the type of the class, that is, something like this: "Usuario"
to be able to do an if and check if it is equal to the type that should be passed to it. Security issues.
So you can know the name of the class, I hope it helps you:
In php5.5 and later you can use the property
class
that all objects have:echo $usuario::class
This returns the fqcn of that class, which would be the namespace and the corresponding class. This will be more useful if you have multiple spacenames and multiple user classes in your project.
Another possibility is through the is_a() function .
This function checks if the object given by
object
is of that class.An example applied to your code:
We verify that the variable
$var
belongs to the classUsuario
, if so, we will do the corresponding operations.