Δευτέρα 26 Νοεμβρίου 2012

Tutorial αναλυτικό και με βίντεο για την αντικειμενοστρέφεια της PHP.

To make things easy, the tutorial is divided into 23 steps.
Step 1:
First thing we need to do is create two PHP pages:
index.php
class_lib.php


OOP is all about creating modular code, so our object oriented PHP code will be
contained in dedicated files that we will then insert into our normal PHP page using
php ‘includes’. In this case all our OO PHP code will be in the PHP file:
class_lib.php
OOP revolves around a construct called a ‘class’. Classes are the cookie-cutters /
templates that are used to define objects.
Step 2:
Create a PHP class
Instead of having a bunch of functions, variables and code floating around willy-nilly,
to design your php scripts or code libraries the OOP way, you’ll need to define/create
your own classes.
You define your own class by starting with the keyword ‘class’ followed by the name
you want to give your new class.
<?php class person {
}
?>
Step 3:
Add data to your class
Classes are the blueprints for php objects – more on that later. One of the big
differences between functions and classes is that a class contains both data
(variables) and functions that form a package called an: ‘object’. When you create a
variable inside a class, it is called a ‘property’.
<?php
class person {
var name;
}
?>
Note: The data/variables inside a class (ex: var name;) are called ‘properties’.
Step 4:
Add functions/methods to your class
In the same way that variables get a different name when created inside a class (they
are called: properties,) functions also referred to (by nerds) by a different name when
created inside a class – they are called ‘methods’.
A classes’ methods are used to manipulate its’ own data / properties.
<?php
class person {
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
Note: Don’t forget that in a class, variables are called ‘properties’.

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου