How to instantiate an object according to a Front End event?

0

Good afternoon, guys.

I do not know if my question makes much sense, but there it goes.

For example, I have a class that has several functions, and I have my Index.php

Here we go, when I load the index and I need to execute a method of a class, how do I instantiate the object of this class and execute the function according to a user action? For example, when the user clicks a button it will instantiate the object and execute a certain function.

Ex:

Class:

<?php
 class usuario{

 private nome;

 public function getNome(){
  return $this->Nome;
 }
}
?>

Index:

...

$usuario = new usuario();
$usuario->getNome();

...

    
asked by anonymous 22.02.2017 / 18:06

2 answers

1

If I get it right, you want to spend a value on an action to know what to do next. Normally you pass parameters by $ _GET in the url, $ _POST via form or breaking the url when using friendly url.

Passing an action through a link to logic is as follows:

Link:

<a href="index.php?acao=carregausuario">Carregar usuário</a>

Code php:

if(isset($_GET["acao"]))
{
    if($_GET["acao"]=="carregausuario")
    {
        $usuario = new usuario();
        $nome = $usuario->getNome();
    }
}

Then just give echo $nome; where you need to display that name.

    
22.02.2017 / 18:58
0

From HTML you can not execute PHP code. What you can do is to use javascript to make an http request for a PHP script that runs what you want.

You can make a call as in the code below in the action you want.

$.ajax({
    type: 'POST', // Ou GET
    url: 'arquivo-com-acao-desejada.php'
});
    
22.02.2017 / 18:40