PHP Object-oriented (i)---2017-04-17

Source: Internet
Author: User
Tags modifiers

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.