Access to PHP objects on other pages

Asked

Viewed 59 times

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');

  • 1

    @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".

  • you urged the class? $usuario = new usuario(); if not, post error message and abs code - Diego Lela 5 mins ago

  • 1

    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.

  • So I’m calling yes to another user class by require_once. And I’ve also instancied $usuario = new user;

  • Please do not publish your codes in image form. The site has support for codes, just use the button { } of the editor.

  • @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.

  • how can I go by reference? I saw something here about serialize with session, that’s the way ?

  • @Take a look at my answer. I used an easier way for you.

Show 4 more comments

1 answer

2


More simply, you can store the created instance of your object Usuario in a session (PHP documentation for session functions) and access them on the desired page. See below:

While on the page Gerente.php

<?php
    $usuario = new Usuario();       

    $usuario->logar();
?>

Do:

<?php
    session_start();

    $usuario          = new Usuario();       
    $_SESSION['User'] = $usuario;

    $usuario->logar();
?>

So on the page index.php, you can access the instance of your object:

<?php
     echo "<h1>" . $_SESSION["User"]->getNome() . "</h1>";
?>

Or on the page index.php:

<?php
     $userObject = $_SESSION["User"];

     echo "<h1>" . $userObject->getNome() . "</h1>";
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.