Language:

Working with Classes and objects in PHP

12 February 2011 | In Web | 2,4 thousand views | By

Click & share: 


codigo phpA class is a set of variables and functions related to these variables. The advantage of its use is to be able to take advantage of the ability of information encapsulation. With the package the user of a class does not need to know how it is implemented, by using the interface, i.e., the functions available. A class is a type, and therefore cannot be assigned to a variable. To define a class, You must use the following syntax:

class Nome_da_classe {
var $variavel1;
var $variavel2;
function funcao1 ($parametro) {
/* === corpo da função === */
}
}

Object

As I said previously, classes are types, and cannot be assigned to variables. Variables of class type are called objects, e devem ser criadas using o operador new, following the example below:

$variavel = new $nome_da_classe;

In order to use the functions defined in the class, the operator must be used “->”, as in the example:

$variavel->funcao1()

The variable $this

In the definition of a class, You can use the variable $ this, which is the object itself. Like this, When a class is instantiated an object, and a function of that object in the class definition uses the variable $ this, This variable is the object that we are using.

As an example of the use of classes and objects, podemos utilizar a classe account, that defines a simple bank account, with functions to see the balance and make a claim.

class conta {
var $saldo;
function saldo() {
return $this->saldo;
}
function credito($valor) {
$this->saldo += $valor;
}
}

$minhaconta = new conta;
$minhaconta->saldo(); //a variavel interna não foi inicializada, e não contém nenhum valor
$minhaconta->credito(50);
$minhaconta->saldo(); // retorna 50

SubClasses

A class can be an extension of another. This means that she will inherit all variables and functions of another class, and still terá as que forem adicionadas pelo programador. In PHP it is not allowed to use multiple inheritance, i.e., uma classe pode ser extensão de apenas uma outra.Para create uma classe extendida, or derived from another, should use the extends keyword, as can be seen in the following example:

class novaconta extends conta {
var $numero;
function numero() {
return $this->numero;
}
}

The above class is derived from the class account, having the same functions and variables, with the addition of the $ variable number and function number().

Builders

A constructor is a function defined in the class that is automatically called when the class is instantiated (via the new operator). The constructor must have the same name as the class to which it belongs. See example:

class conta {
var $saldo;
function conta () {
$this.saldo = 0;
}
function saldo() {
return $this->saldo;
}
function credito($valor) {
$this->saldo += $valor;
}
}

We can realize that the class account, has a constructor, that initializes the variable $saldo with the value 0.

A constructor can contain arguments, that are optional, o que torna esta tool mais poderosa. In the example above, the class constructor can receive account as an argument a value, that would be the initial value of the account.
It is worth noting that for derived classes, the constructor of the parent class is not automatically inherited when the constructor of the derived class is called.

In Ivaiporã-PR, Computer engineer, Workgroup Administrator Tips in General. Passionate about technology and Informatics.



Leave your comment

A bit about us

    The Group generally appeared in Tips 2007 from innovative ideas on troubleshooting problems faced daily by those who use the technology and computer science, both ordinary users and technicians. But where did, why and what is the purpose of this site?

Click here to read!

Siga o Dicas em Geral no Google+

Video of the week