-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?
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).
– Woss
Why not first try to do only with PHP and then, already dominating this part, do the integration with the graphical interface?
– Woss
@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.
– Jorge Brito