Troubleshooting object instantiation based on role in PHP app

31 Views Asked by At

I am attempting to instantiate an object based on the role of setting the data. Here is my code.

<?php
// Incluye la definición de las clases CUsuario y CDocente
include_once('modelo/CUsuario.php');
include_once('modelo/CDocente.php');

// Verifica si el usuario está autenticado (aquí debes tener tu lógica de autenticación)
if (isset($\_SESSION\['usuario'\])) {
// Obtiene los datos del usuario de la sesión
$usuario = $\_SESSION\['usuario'\];

    `<?php // Incluye la definición de las clases CUsuario y CDocente include_once('modelo/CUsuario.php'); include_once('modelo/CDocente.php');
    
    // Verifica si el usuario está autenticado (aquí debes tener tu lógica de autenticación) if (isset($_SESSION['usuario'])) { // Obtiene los datos del usuario de la sesión $usuario = $_SESSION['usuario'];
    // Obtiene el rol del usuario
    $rol = $usuario->getRol();
    
    // Instancia el objeto según el rol del usuario
    if ($rol === 'Alumno') {
        $usuario = new CUsuario();
    } elseif ($rol === 'Docente') {
        $usuario = new CDocente();
    }
    
    // Obtiene los valores del correo y la contraseña
    $correo = $usuario->getCorreo();
    $passwd = $usuario->getPasswd();
    
    // Obtiene otros datos del usuario si los necesitas
    $nombre = $usuario->getNombre();
    // Otros getters...
    
    // Imprime los valores
    echo "Correo: " . $correo . "<br>";
    echo "Contraseña: " . $passwd . "<br>";
    echo "Apepat: " . $usuario->getFecnac() . "<br>";
    // Otros datos del usuario...
    $usuario->setDatos($usuario->getNum($correo, $passwd));
    echo "<br>Nombre: " . $nombre . "<br>";

} else {
// Si el usuario no está autenticado, puedes redirigirlo a la página de inicio de sesión
// o mostrar un mensaje de error, dependiendo de la lógica de tu aplicación
echo "El usuario no está autenticado.";
}

?>

Error

Fatal error: Uncaught Error: The script tried to call a method on an incomplete object. Please ensure that the class definition "CDocente" of the object you are trying to operate on was loaded before unserialize() gets called or provide an autoloader to load the class definition in C:\xampp\htdocs\revistaEq1\vista\vstCuenta.php:12 Stack trace: #0 C:\xampp\htdocs\revistaEq1\control\ctrGeneral.php(21): include() #1 C:\xampp\htdocs\revistaEq1\index.php(29): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\revistaEq1\vista\vstCuenta.php on line 12

The error only appears when the role is 'teacher'. When the role is 'student', it works well, creating the object and setting the data. That's why I suggest that the problem might be in the 'teacher' object or this line: $rol = $usuario->getRol(); Responses can be in Spanish or English.

I strive to avoid errors, create the 'teacher' object, and set my data without issues. I hope someone can recognize this problem and know how to resolve it. Please let me know if you need more information about the project or classes.

0

There are 0 best solutions below