PHP lessons - introduction to classes and objects - part I

You can think of a class as a needful template that defines an object. It contains functions known as class methods and variables known as class properties/attributes. They are much like the normal php functions and variables:
//example of class property
public $my_name = "test";

//example of class method
function my_method() {
echo "Hello!";
}
Here is an example of a simple class structure: Just copy and paste it in your favorite editor:
<?
class my_class {//begin of class definition

//here we place its functions and variables
public $my_name = "test"; //we are directly setting value of "test" to variable $my_name

function my_method() {
echo $this->my_name; //we are accessing the above class variable $my_name
}

} //end of class my_class
?>
(Note that we've changed the function my_method(). This way when evoked it will display the contents of $my_name. The arrow symbol ( -> ) points to a method or property of a given object. To access our current object, we use the special name $this.)

Next lets create a new object or in other words to make an instance of my_class:
$my_object = new my_class(); //create object from class my_class accessible through variable $my_object
Now the variable $my_object holds a reference to the newly created object of class $my_class. So we could try its methods and properties:
$my_object->my_method(); //will display "test"
$my_object->my_name = "Peter";
echo "<br />";
$my_object->my_method(); //will display "Peter"

Initialization
If we want to initialize our class(i.e. to run some functions or set up some variables) we use its special function called __construct(). Upon exiting our class we use its function __destruct(). Also in the example we restrict the access to our class variable.
Here is an example:
<?php class User{

private $name; //variable $name will be accessible only via methods set_name() and get_name()

function __construct($default_name = "anonymous"){ //initialize $name = "anonymous";
$this->name = $default_name;
}

function set_name($new_name){
$this->name = $new_name; //set $name via incoming parameter $new_name;
}

function get_name(){
return $this->name; //access contents of $name;
}

}

//create object $my_user or make an instance of User
$my_user = new User();

echo "Initial name: " . $my_user->get_name();
echo "<br />";

//now we change $my_user
$my_user->set_name("Ted");
echo "New name: " . $my_user->get_name();
?>

As you can see we've set up out User class: have initialized its default $name via the constructor __construct(), and we have built two functions to manipulate its name(get_name and set_name). Remember that variable $name now can only be accessed by the class methods get_name() and set_name() and not directly: echo $my_user->name will produce an error.

Why private?
Well, it's really a practical question. And the answer is simple: you use 'private' when you have member variables that depend on each other(i.e must change accordingly).
for example if you have:
class User {
public $name; //public means open to direct access
public $avatar;

function set_name($new_name) {
$this->name = $new_name;
$this->avatar = "My name is $this->name. Nice to meet you!";//dependent variable
}
}
$my_user=new User();
$my_user->set_name("John");
$my_user->name="test";
echo $my_user->name;
echo $my_user->avatar;
?>

when $name is 'public' you are allowed to use the direct: $User->name = "John";
but this way you'll ONLY change the variable $name and user's $avatar will not be updated!
Solution: Just set $name to 'private' to disable access such like $User->name and use member function set_name() that automatically updates the dependent variables for you. Now of course you must use the function get_name(); to get the value of variables $name and avatar like for example:
function get_name() {
echo $this->name;
echo "
";
echo $this->avatar;
}
Here is part I of the video from this tutorial:



In lesson 2 we'll explore more advanced topics and practical usage of PHP classes and objects. by Nevyan Neykov



0 коментара:

Post a Comment