0
I’m starting to program object orientation in PHP. I came across the following problem: I have a class Usuario
and I need to access the object as soon as I do login on the site, however, when I try to access the object and function of other pages, it gives error.
How do I access the object on another page?
$Usuario->metodo();
we have the user class and then down
<?php
include_once("Conexao.php");
class Usuario extends Conexao
{
private $id;
private $nome;
private $senha;
private $sessao;
function setSessao($sessao){
$this->sessao = $sessao;
}...
the manager page I enter and the user class and log in
<?php
$usuario = new Usuario();
$usuario->logar();
?>
the index class to which I want to use the instantiated object on the manager page
<?php
echo "<h1>".$usuario->getNome()."</h1>";
?>
the error that appears is this : Notice: Undefined variable: usuario in
C:\xampp\htdocs\ConstrutoresFree\MyBiz\index.php on line 221
Fatal error: Uncaught Error: Call to a member function getNome() on null in C:\xampp\htdocs\ConstrutoresFree\MyBiz\index.php:221 Stack trace: #0 {main} thrown in C:\xampp\htdocs\ConstrutoresFree\MyBiz\index.php on line 221
(thanks for the tips)
Are you calling the class on the page that receives the login data? Example:
require_once('Usuario.php');
– Laércio Lopes
@Hamilton, can you explain the problem further? Preferably put all the code you are using regarding class instantiation and explain better what this "other file".
– Woss
you urged the class?
$usuario = new usuario();
if not, post error message and abs code - Diego Lela 5 mins ago– Diego Lela
There’s
N
possibilities. Your question is not clear enough, I suggest you edit it and add information like the code used and mainly the error that is being displayed.– Marcelo de Andrade
So I’m calling yes to another user class by require_once. And I’ve also instancied $usuario = new user;
– Hamilton Ventura
Please do not publish your codes in image form. The site has support for codes, just use the button
{ }
of the editor.– Woss
@Hamiltonventura Amigo, post what error is being generated. Addendum: The object you prompted in Login will not be available on other pages unless you pass a reference from it.
– Godfrey the King
how can I go by reference? I saw something here about serialize with session, that’s the way ?
– Hamilton Ventura
@Take a look at my answer. I used an easier way for you.
– Godfrey the King