How to invoke a method specific to a PHP class?

Asked

Viewed 1,928 times

-2

I created a class Pen which has the following attributes and methods:

<?php

class caneta {
    var $modelo;
    var $cor;
    var $ponta;
    var $carga;
    var $tampada;

    function rabiscar() { 
       if ($this->tampada == true) {
           echo "<p>ERRO! Não posso rabiscar.</p>";
       }  else {
           echo "<p>Estou Rabiscando...</p>";
       }
    }

    function tampar() {
        $this->tampada = true;

    }

    function destampar() {
        $this->tampada = false;
    }
}

?>

With another file index.php, wanted to know how to call the method tampar() class pen, for example using onClick on a button, so when the user clicks on the method button tampar() is invoked and the value of the attribute capped switches to true.

What would be the right method to do this?

My situation is as follows: I have a pen.php file, and in this file is a class called Pen and this class has the attribute capped, the method cap() and the getTampar method (i.e., returns the state of the pen: capped or uncapped):

class Caneta {
   public $tampada;
   public function tampar() {
       $this->tampada = true;
   }
   public function getTampada() {
   if ($this->tampada == true) {
       return "Tampada";
   } else {
       return "Destampada";
    }
 }  
}

Inside another index.php file I have a tag to and an instantiated pen class object:

<?php
  require_once 'caneta.php';
  $caneta = new Caneta;
  echo '<a href = "#" id = "a" onclick = "ChamarMetodoTampar()">Tampar 
  caneta</a>';
  echo 'A caneta está {$caneta->getTampada}.';
?>

My question is: how to call the method Cap of the pen class with the onclick property, thus causing the text contained in echo to change?

  • 1

    The fact that you are working with a single button onclick indicates that you have a graphical interface, possibly written in HTML. If so, there are many aspects involved in your problem that are unclear, as it will require you to handle the DOM event from your Javascript interface and communicate in some way with your server running PHP. Since you’re a beginner, we can’t assume that you already know how to do all this, so the problem would no longer be just about PHP and object orientation (which isn’t even that actually, just creating a class doesn’t imply being using POO).

  • 1

    Why not first try to do only with PHP and then, already dominating this part, do the integration with the graphical interface?

  • @Andersoncarloswoss, I am creating this html site with PHP. I updated my question to make it clearer. Maybe it’s not POO, but when I started studying about PHP, it was researching about POO (sorry my lack of knowledge), anyway, I researched about how to do this and only found to do it via AJAX , but I don’t know how to apply AJAX to my code or what changes I should make.

1 answer

0

One very important thing that ends up not being mentioned when working with PHP is that every HTTP request is a STATELESS request, that is, everything you do in that request, if it is not persisted in a database or in an environment variable or in a file will be lost, example:

Assuming you have an index.php that has the pen aim and a file called ver_result.php that also uses the pen object, on each page is a different object so if you "close the pen" in the index it will not close in the ver_result!

This is a fairly common mistake, so what you can do to test whether this is working properly or not would be the following:

<? 
require_once "caneta.php";
$caneta = new caneta();
$caneta->tampar();

// aqui vai dar erro pois a caneta foi previamente tampada
$caneta->rabiscar();
?>

you should directly access the url of your test php file, without trying to do this via HTML button, because as HTTP said is a stateless protocol.

Browser other questions tagged

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