I suggest studying more deeply object orientation, the example below is something very superficial and teaches little.
First we need to create a class with the name pais
.
namespace lib
/**
* Classe Pais
* @package lib
*/
class Pais
{
/**
* @var string $iso;
*/
private $iso;
/**
* @var string $nome;
*/
private $nome;
/**
* Atribui um valor a propriendade $iso
* @param string $iso
* @return lib\Pais
*/
public function setIso($iso)
{
$this->iso = $iso
$return $this;
}
/**
* Retorna o valor da propriedade $iso
* @return string
*/
public funcion getIso()
{
return $this->iso;
}
/**
* Atribui um valor a propriendade $nome
* @param string $nome
* @return lib\Pais
*/
public function setNome($nome)
{
$this->nome = $nome
$return $this;
}
/**
* Retorna o valor da propriedade $nome
* @return string
*/
public funcion getNome()
{
return $this->nome;
}
}
First we assign a namespace
the class. Namespace enables the grouping of classes to avoid conflict between their names, acting as an encapsulator, that is, its functioning is equivalent to that of directories in operating systems, where two files with the same name cannot exist in a single directory, but there may be two files with the same name located in separate directories.
Note that the class has comments and some started with @
. These comments serve to standardize and generate automatic documentation of your code, using tools such as phpdoc. In addition to the documentation, the comments serve to help programmers who can in the future change their code, making it easier to understand it.
See that in the class we define the methods get
and set
for each property. Hence arises a natural question: why create methods to access variables, if we can access them directly? - For safety reasons.
The variables private
can only be accessed from within the class, i.e., cannot be accessed outside the scope of the class. For example, imagine within the method setIso
we would have a check if the code actually exists; if the property were public
, we could simply set any value to property iso
without any validation.
class Pais
{
// Propriedade pode ser acessada fora do escopo da classe Pais
public $iso;
}
$pais = new Pais();
$pais->iso = 'QualquerValorParaIso';
When trying to access a private property outside the scope of the class, the following error will occur:
class Pais
{
// Propriedade não pode ser acessada fora do escopo da classe Pais
private $iso;
}
$pais = new Pais();
$pais->iso = 'QualquerValorParaIso';
Error: Cannot access private Property lib Pais::$iso (500 Internal Server Error)
Clarified (I hope) some concepts, we will now create a new aquivo where we will make use of the class Pais
.
php test.
use lib\Pais
$paisA = new Pais();
$paisA
->setName('Brasil')
->setIso('BRA')
;
$paisB = new Pais();
$paisB
->setName('Portugal')
->setIso('PRT')
;
$paisC = new Pais();
$paisC
->setName('Portugal')
->setIso('PRT')
;
The above code generates 3 instances of the class Pais
, that is, 3 objects distinct and independent of each other, each with its own scope.
In a very simple way, we can verify if the values defined are equal between the objects through the method getIso
:
$isoA = $paisA->getIso(); //BRA
$isoB = $paisB->getIso(); //PRT
$isoC = $paisC->getIso(); //PRT
//output: não
echo 'Iso país A==B', $isoA==$isoB ? 'sim':'não';
//output: não
echo 'Iso país A==C', $isoA==$isoC ? 'sim':'não';
//output: sim
echo 'Iso país B==C', $isoB==$isoC ? 'sim':'não';
The above method is ineffective if there is a collection of countries, test all combinations generating one row for each if
or even we may not know how many items there are in the collection, etc... I used the above method only to demonstrate how to create an instance of the class (which results in an object) and use the respective methods.
Each case is a case and should be studied in order to be implemented in the best possible way, but for now the example serves.
Already have some part of the code made ? Why if yes,post so we can help you better in this issue.
– Falion
You already have at least the class?
– KaduAmaral