PHP Object-oriented and instantiated objects

Source: Internet
Author: User
Tags opening and closing tags

This article mainly introduces about the PHP object-oriented and instantiation of the object, has a certain reference value, now share to everyone, the need for friends can refer to

Class

Statement

[Modifier] class class name {    [property]    [Method]}

Precautions:

1) class name follows 大写 The Hump naming specification at the beginning

2) The opening and closing tags of curly braces must be in line.

Modifier

Private     protected      public    var  (not recommended)

Access rights

Instantiation of

Instantiate using the New keyword.

Brackets are not added.

Example

1. Declare class dog{    //2. Define Properties public    $name = ' Wang Choi ';    public $sex = null;    3. Define method public    function bark ()    {        echo ' bark ~ ';    }} 4. Instantiate $d1 = new Dog ();

Instantiating an Object

Calls to properties, methods

Use to -> access non-static properties | methods.

Example

Declare class dog{    //define Properties public    $name = ' Wang Choi ';    public $sex = null;    Define method public    function bark ()    {        echo ' bark ~ ';    }} Instantiate $d1 = new Dog ();//Use Properties and methods echo $d 1, Name;echo ' <br/> '; echo $d 1, bark ();

Internal call

Method can use a pseudo-variable when it is called inside the class definition $this .

$this, it represents the object being used.

Example:

Declare class dog{    //define Properties public    $name = ' Wang Choi ';    public $sex = null;    Public Function Intruduce ()    {        echo ' My name is '. $this->name;    }}

Pass Assignment

1) Simple type , pass assignment to both sides, 相互独立 .

2) When a composite type is passed, the assigned value is the identifier of the object , so 变化一致 .

Simple Type examples:

$a = ' abc ';//pass Assignment $b = $a;//Change A's value $ A = ' QQ '; Var_dump ($a)   ; Qqvar_dump ($b);   Abc

Composite type Examples:

Class myclass{public    $str = ' abc ';} $a is the identifier of the object $ A = new MyClass ();//pass assignment is  equivalent to assigning the identifier to $b$b = $a; Var_dump ($a)    ; Abcvar_dump ($b);    abc//change the value of the A object str property $ A-str = ' 111 ';//$a and $b are identifiers var_dump ($a);    111var_dump ($b);    111//change the value of a itself $ A = 123;var_dump ($a);    123var_dump ($b);    111   Object Identifiers

Reference assignment

Whether it is a simple type or a type, the two sides are all in the same line of change.

Example:

$a = ' abc ';//reference Assignment $c = & $a; var_dump ($a)   ; Qqvar_dump ($c);   Qq

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.