Focus: Defining classes and instantiating classes; access modifiers; constructors
First, object-oriented
The main difference between orientation and process-oriented is that the former contains the concepts of classes and objects
ii. Classes and Objects
1. Classes are abstract (summed up) by many objects that represent the attributes of all objects
2, the object is by the tired instantiation of something has its own characteristics of the real existence of all objects
3. Defining Classes
Class plus category name (first capitalization) {}
eg
Class Ren
{
var $name; ---- member Variables
var $age;
function eat ()----- member method or function
{
}
}
4.instantiation ( because the class is abstract and cannot be used directly)
$r = new Ren (); -------instantiation of a person
$r->name = "Zhang San"; ---------Calling member variables
$r->age = 1;
$r->eat (); ---------Call member Methods
$r 1 = new Ren ();-------instantiation of the second person
$r 2 = new Ren ();-------instantiate a third person
5, access modifiers are very important ( member variables are generally private, member methods are generallypublic)
(1) Public
Class Ren
{
Public $name;
Public $age;
Public function Eat ()
{
}
}
(2) Protected protected can only be accessed in this class and in subclasses of that class
(3) Private private access only in this category
Class Ren
{
Private $name;
Private $age = 2;
Public function Eat ()
{
Echo $this->age; ---This represents the object (who calls the ear () represents the object);
}
}
$r = new Ren ();
third, the constructor function ( whether written or not, there is no writing, just can't see it)
Used to initialize variables default to public
1. Features
(1) Special wording
function class name () {}-----------used before, and it's right now.
function__construct () {} ------------now used
(2) Execution time special -----------executed at instantiation
EG1:
Class Ren
{
Private $sex = "male";
function __construct ()
{
$this->sex;
}
Public function Eat ()
{
}
}
$r = new Ren ();
$r->eat ();
EG2: With parameters
Class Ren
{
Private $sex;
function __construct ($s)
{
$this->sex= $s;
}
Public function Eat ()
{
}
}
$r = new Ren ("female");
$r->eat ();
PHP Object-oriented (i)---2017-04-17