Language:

Working with Classes and objects in PHP

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

Click and share: 

codigo phpA class is a set of variables and functions related to these variables. The advantage of using it is to be able to take advantage of the resource of information encapsulation. With the package the user of a class does not need to know how it is implemented, by using the interface, IE, 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 was stated earlier, 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, that is the subject. 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 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 yet terá as que forem adicionadas pelo programador. In PHP it is not allowed to use multiple inheritance, IE, uma classe pode ser extensão de apenas uma outra.Para create uma classe extendida, or derived from another, must use the reserved word extends, 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 the function number().

Builders

A constructor is a function defined in the class that is automatically called when the class is instantiated (through the new operator). The constructor must have the same name as the class to which it belongs. See the 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 $ balance with the value 0.

A constructor can contain arguments, that are optional, o que torna esta tool mais poderosa. In the example above, the constructor of the class account can receive 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, Group administrator Tips in General. Passionate about Technology and computer science.



Leave your comment

A bit about us

    The General Tips Group arose in 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 know!

Give a +1 in Tips in General
     

Video of the week

Copyright (cc) 2014 - General Tips group - Privacy - Site map - Layout of the site produced by William Périgo - Created by Amilton Junior