Access instantiated class attributes and methods in another class

Asked

Viewed 681 times

4

Before the structure below:

  • class view
  • class controller
    • new view()
  • class controllerUsuario extends Controller
    • public $usuario;

as access $usuario from the "View"?

  • 1

    create a namespace and within the view use nomeNameSpace

  • Good Rafael, I solved with namespace. It worked, Valew!

  • I posted as a reply, please mark as resolved so that other users can find this solution.

2 answers

3


You can use namespace to use classes in other files.

use RaizDaPasta\pastaDaClasse\Classe;

Inside your file where you do the use install a new object and use the function you want

0

I’m not sure what you want to do, but I believe it must be something like this:

<?php

class Apresentar
{
    public function __construct(ControllerUsuario $usuario)
    {
        echo $usuario->getUsuario();
    }
}

class ControllerUsuario
{
    private $usuario = 'Fábio';

    public function getUsuario()
    {
        return $this->usuario;
    }
}

new Apresentar(new ControllerUsuario);

Browser other questions tagged

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