In its beginnings, PHP was not a programming language or Object Oriented OOP is its acronym in English, with advancing technologies and new versions of PHP was adding several functionalities of the OOP methodology.
Divide this tutorial in the following sections, which are those that define whether a language is object oriented or not:
· Classes and Objects
· Methods and attributes
· Class Inheritance
· Constructors and Destructors
· Encapsulation
Classes and Objects:
Unlike traditional programming, object-oriented programming is based on classes and objects.
A class contains attributes or properties and methods that are functions. This is very important to be clear. Function method is synonymous. Property attribute is synonymous.
The class would be something like a cast from that mold and creating objects.
In PHP define a class as follows:
Customer class
{Using the above lines of code defines the class "Customer" which we use for our tutorial.
/ / Here goes the code for our class
}
While this tutorial is not focused on defining the concepts of OOP, it is good to clarify that the name of the class should not be in the plural, for this reason named "Customer" instead of "clients".
Methods and Attributes
The methods and attributes are defined "code" within the class. The class attribute is a property.
Attributes:
The order programming and tidiness are very important. Therefore, we start analyzing our kind of "up" "down". The first thing is defined in a class are attributes.
For example, in our example class "Customer" attribute would be "Name," "Last Name", "Age" etc..
If our example was a class "Car", the attributes would be: "wheel", "brand", "model" and so on. Need more clear now?
Okay, let's move on defining the attributes of our class, for which use the keyword "private" or if you work with PHP versions less than 5.0 will need to use "var", so our class tango will look as follows:
class Customer {/ / These values below are the attributes or properties of ourIn other words, attributes are visible and accessible within the class itself.
If we follow the standard OOP must define the attributes and private. In other words, are accessible only from within the class itself, so tango use "private". In other words, no other class in any other piece of code can change an attribute belonging to
Methods or Functions:
One method is the same as a function, is a portion of code within the class that performs a task and can return a value or not.
The first methods we defined in our class are those which assign values to our attributes. At this point there is always discrepancy in the programming community. The discussion is whether to create a method for each attribute or attributes to create public and thus avoid the definition of individual methods. This may sound confusing, we will see an example:
So you can see the difference you need to see first-consuming way more code to that then you can compare. So, back to the methods, we define two is responsible for allocating and returning a value to the property name.
Methods to control our attributes.
Add the following code immediately after our properties.
function setName ($ val)The class "Client" with the methods would be as follows:
{
$ This -> = $ val mNombre;
}
function getName ()
{
return $ this -> mNombre;
}
class Customer {/ / this below are the attributes or properties of our that assigns a value to the property name {$ this -> = $ val mNombre;} function getName () / / method that returns the value assigned to the property name {return $ this -> mNombre;}}The method "setName" receives a parameter value using "$ this" assigned. Remember, "$ this" is used only within the classes.
The method "getName" works in reverse and returns the value of the property, we use "return" to return it.
The example above is highly recommended, but requires more lines of code, which translates into more time.
Only now would be able to test with our class. Then we will see the second way to assign values to attributes of the class.
Testing our class:
To prove what we have been learning so far creat a php page called test.php and add the following code:
require ("classes / Cliente.php") / / create our first client object $ client = new auxCliente () / / assign a name to our Customer object $ auxCliente -> setName ("Ariel") / / create Our second client object $ client = new auxCliente2 () / / assign a name to our client $ auxCliente2 second object -> setName ("Peter") / / retrieve the name assigned by the getName method echo "The name of the first customer is: ". $ auxCliente -> getName ()." <br> "echo" The name of the second client is: ". $ auxCliente2 -> getName ()." <br> ";Analyzing a short code, the first line does is include the file that has the class "Customer", once included in "test.php" and we can use.
Step 1 to use the class:
The first thing to do to use the class is to "create an instance" of it. This gives us a model of the object or class, thus: "$ auxCliente = new Customer ();" we create an object called "" $ auxCliente "class" Customer ". Remember it is an object and this "gap" in everyday language. It is a mold.
Using a more human terms, the client has no name, surname, telephone and so for the moment.
Step 2, assign values to the attributes:
By having our object and we can assign a name to the client, we use the corresponding method as follows:
/ / Assign a name to our Customer objectWe have a client now create another follows.
$ AuxCliente -> setName ("Ariel");
/ / Create object for our second clientThe way to create our second client is similar to the first, only now use a new object "$ auxCliente2". Remember, whenever you want to get a new object, you must use "new Object ()" in our example "new Customer ()".
$ AuxCliente2 client = new ();
/ / Assign a name to our second Customer object
$ AuxCliente2 -> setName ("Peter");
Step 3, verify operation of the class:
Once the name assigned to the two customers really see that each customer has his name and that each customer is a different object.
I checked as follows:
/ / Retrieve the name assigned by the method getNameThe result is:
echo "The name of the first client is:". $ auxCliente -> getName (). "<br>";
echo "The name of the second client is:". $ auxCliente2 -> getName (). "<br>";
/ / The name of the first client is: ArielReturning to the issue related to whether we should use methods to control the properties of our class or not, we use some changes in our class.
/ / The name of the second client is: Peter
As you've seen how to assign a value to the property "mNombre" was using the "setName" and retrieve the value using the method "getName" are not things that other public functions defined within the class.
There is always a way not recommended to control the attributes without using methods that is defined as public property, for which we can use "var" or "public" rather than "private."
The definition of the properties would look like:
Customer classAs you have seen our class has no methods "set" and "get" that took care of the properties.
{
/ / This below are the attributes or properties of our class
public $ mNombre;
public $ lastNameValue;
public $ mDireccion;
public $ MPaĆs;
public $ mTelefono;
public $ disease;
public $ mFechaNacimiento;
}
How do we control the properties without the methods?
Since the properties are defined as public can access them from anywhere in the code. That is, from within the class by using "$ this" and without.
We prove our second example with the following lines of code:
require ("classes2/Cliente.php") / / create our first client object $ client = new auxCliente () / / assign a name to our Customer object $ auxCliente -> mNombre = "Ariel" / / create object for our second $ client = new Client auxCliente2 () / / assign a name to our second $ auxCliente2 Customer object -> mNombre = "Peter" / / retrieve the name assigned by the getName method echo "The name of the first customer is: ". $ auxCliente -> mNombre." <br> "echo" The name of the second client is: ". $ auxCliente2 -> mNombre." <br> ";Given that public properties are assigned a name in the same way we assign a value to a common variable in PHP.
$ AuxCliente -> mNombre = "Ariel";The same to recover the value of the property, accessed directly and "$ auxCliente-> mNombre"
All well and good. Now, what is the best way?
This is very relative and depends on the application you're developing. What is certain is that if you use the simplest method you would be out of the development standards of object-oriented applications. There are companies that are very strict on these methodologies and their programmers require the application of these rules to make the final source is uniform.
This tutorial is intended to be as professional as possible, so we show the two forms. Which depends on the programmer to use.
What other use have their methods?
The methods are not only useful for managing the properties. In fact this is a less important tasks performed by the methods.
We use a method to calculate the age of the person on the date of birth.
Add the following to our class:
setFechaNacimiento function ($ val) / / method that assigns a value to the property Birthdate {$ this -> = $ val mFechaNacimiento;} function getEdad () / / method that returns the value assigned to propertyThe "calcularEdad" is committed to give a customer's age based on date of birth. It is certainly a clear example of how we can use the methods.
We will test the method by adding the following PHP code into a blank page:
require ("classes / Cliente.php") / / create our first client object $ client = new auxCliente () / / assign a name to our Customer object $ auxCliente -> setName ("Ariel") / / assign $ birthdate auxCliente -> setFechaNacimiento ("19/11/1979") / / calculate age $ auxCliente -> calcularEdad () / / retrieve the name assigned by the getName method echo "Customer:". $ auxCliente -> getName (). "has". $ auxCliente -> getEdad (). "<br> years";With the latter finished the chapter related to methods and properties.
Class Inheritance
Without doubt one of the most prominent features in the object-oriented languages, and PHP has this feature.
When we speak of inheritance we refer to methods and properties that are "imported" from another class. As the word implies, inherited functions are already made.
The heritage helps us to save and reuse things work already done.
For example, if in our company we have clients, suppliers and employees. Everyone has things in common, they all have name, phone, country etc..
Taking it to the analytical level, they are all people. Therefore, we can define a class "Person", which contains all the attributes and methods common to the customer, supplier and employee.
We can say that these classes inherit the attributes of common person.
To begin with we define the Person class:
class Person {/ / this below are the attributes or properties of our that assigns a value to the property name {$ this -> = $ val mNombre;} function getName () / / method that returns the value assigned to the property name {return $ this -> mNombre;}}And our class "Customer" look like:
require ("Persona.php");At first glance it looks empty, with no methods or properties. But if we pay attention, we inherit the word for "extends" and then "Person." This, in other words means that the Customer class inherits all the properties and methods from Person.
class Customer extends Person
{
}
To demonstrate the utility of the estate will use another class called "" Employee ".
require ("Persona.php");Inheriting we avoid having to define the methods and properties for each class separately, thus avoiding duplicate code and made the most of reuse, one of the basic principles of this methodology.
class Employee extends Person
{
}
To prove that inherited Customer and Employee uses the examples of the previous section.
Constructors and Destructors
Whenever you run "$ auxCliente = new Customer ();" we say we build the Customer object. It may be the case we might want to perform certain actions in the moment of creation or construction of the object. For this we use the constructor, which is just a function that is executed automatically.
In PHP5 the constructor is defined with "__construct" in earlier versions, the constructor is defined with a function with the same name of the class. For example in our class "Customer" the builder would be defined as follows:
/ / Class constructor function __construct ($ val) {$ this -> = $ val mFechaNacimiento, $ this -> calcularEdad ();}The example uses the constructor to automatically calculate the age after instantiating the object.
/ / Create our first client object $ client = new auxCliente ("11/19/1979") echo "Age of client:". $ AuxCliente-> getEdad (). "Years & lt; br & gt;"The destructor of a class is used to perform operations when the object is destroyed, ie when it is removed from memory. This feature is commonly used in chat systems. Where we need to update the database as soon as the visitor closes the page.
Encapsulation
It is the ability of the objects have other objects within itself. A good example might be that a company has customers. Therefore, a client object within the object could be firm.
We create our class 'Company'
class Company {private $ mCliente; function agregarCliente ($ unCliente) / / method that adds the client {$ this -> = $ unCliente mCliente;} function getCliente () / / method that returns the customer {return $ this -> mCliente; }}In a test file add the following code:
require ("classes / Cliente.php") require ("classes / Empresa.php"), $ auxEmpresa = new Company (); / / create our first client object $ client = new auxCliente () / / assign a name to our Customer object $ auxCliente -> setName ("Ariel") / / assign $ auxCliente date of birth -> setFechaNacimiento ("19/11/1979") / / calculate age $ auxCliente -> calcularEdad (); / / add the customer within the company $ auxEmpresa -> agregarCliente ($ auxCliente) / / retrieve the name assigned by the getName method echo "Customer:". $ auxEmpresa -> getCliente () -> getName (). " has ". $ auxEmpresa -> getCliente () -> getEdad ()." <br> years ";The method "agregarCliente" introduces the Customer object in the object company and "getCliente" return it to us.
Hence the following:
$ AuxEmpresa -> getCliente () -> getName ()It would be something like this in pseudocode: Company-> Customer-> Name.
Greetings to all and hope that the tutorial is helpful.
0 comments:
Post a Comment