5
In my course, I’m learning getters which takes "data" and setters, inserting/modifying.
I made my code like this for the pen class:
<?php
class Caneta {
public $modelo;
private $ponta;
public function getModelo(){
return $this->modelo;
}
public function setModelo($m){
$this->modelo = $m;
}
public function getPonta(){
return $this->ponta;
}
public function setPonta($p) {
$this->ponta = $p;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Aula 02 POO</title>
</head>
<body>
<pre>
<?php
require_once 'Caneta.php';
$c1 = new Caneta;
$c1->setModelo("BIC");
$c1->setPonta(0.5);
print("Eu tenho uma caneta {$c1->getModelo()} com a ponta {$c1->getPonta()}");
?>
</pre>
</body>
</html>
In case those little words there, set and get, could I trade for any other? For example (only doubt), could I do so?
<?php
class Caneta {
public $modelo;
private $ponta;
public function puxarModelo(){
return $this->modelo;
}
public function inserirModelo($m){
$this->modelo = $m;
}
public function puxarPonta(){
return $this->ponta;
}
public function inserirPonta($p) {
$this->ponta = $p;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Aula 02 POO</title>
</head>
<body>
<pre>
<?php
require_once 'Caneta.php';
$c1 = new Caneta;
$c1->inserirModelo("BIC");
$c1->inserirPonta(0.5);
print("Eu tenho uma caneta {$c1->puxarModelo()} com a ponta {$c1->puxarPonta()}");
?>
</pre>
</body>
</html>
I’m learning from get and set, but wanted to know if this is standard/mandatory, or if I can change (if I want) no problems?
Default is a reference, if someone tampers with your code they will know that get and set do certain action. But you can quietly switch to what suits you.
– Don't Panic