PHP class and constructor parsing

Source: Internet
Author: User
About the class we all have a certain understanding here only in PHP class noteworthy places

The creation of----class----

PHP uses the keyword class to create a class and uses a pair of curly braces
Such as:

Class name{public  $n = "";  Private $u = "";  Public Function name () {    $n = "233";    $u = "23333";  }  Public function Rename ($newn) {     $this->n= $newn;//this represents this class  }}

The end does not score the number. $n, $u is a field; name () is a constructor (__construct () can also define a constructor, as described below), you can assign a value to a field; Rename () is a method.

----field----

Comparison
$obj =new name ();
Echo $obj->n;
And
$obj =new name ();
Echo $obj->u;

The former can be executed, and the latter cannot be declared private before $u. This is similar to C + +.

Code:

public static $nm = "2333333333333333";

A static field is declared for the function.
Through the class name and:: You can access the variable directly

echo Name:: $nm;

This is similar to C + +.

In PHP, you can also access static fields in a class by self::+$+ variable names, where self is equivalent to $this->.

The use of methods is similar to fields


----constructor----

Constructors have the same name as classes in PHP5 and earlier versions


In PHP5 and later versions, the magic word __construct () can define constructors

Magic word __construct () to define constructors

Class name{public  $n = "";  Private $u = "";  Public Function __construct () {    $n = "233";    $u = "23333";  }  Public function Rename ($newn) {     $this->n= $newn;  }}

Constructors can have parameters

__construct ($name = "", $sex = "man", $age =0) {}

When declaring an object

$obj = new name ("I", "man", 28);

If no argument is given, the default is = after value.

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.