2010 the latest PHP class of lean inductive _php tutorial

Source: Internet
Author: User
One: Structure and invocation (instantiation):

Class classname{}, called: $obj = new ClassName (), and when the class has a constructor, it should also pass in the parameter. such as $obj = new ClassName ($v, $v 2 ...);

two: Constructors and destructors :
1. Constructors are used for initialization: Use __construct (), with parameters.
2, but the destructor cannot take parameters (used to perform some operations or functions before the pin goes to a class). Destructors are named with __destruct (). At the end of the script execution, PHP will pin out the objects in memory, so it is not necessary to analyze the functions, but some, such as cookies, should be used to pin off the function.
Knowledge Points: Constructors are also provided in PHP4, but use a class method with the same name as the class, which is still compatible with PHP5, and when a class does not contain __construct, it looks for a method with the same name as the class, and if found, it is considered a constructor, as follows:
Class Test
{var $b;
function test () {$this->b=5;}
function Addab ($c) {return $this->b+ $c;}
}
$a = new test (); Echo $a->addab (4); Returns 9
3. PHP does not automatically call the parent class's constructor (which does not support constructor overloading) and must be explicitly called using the parent keyword.
Class employee{
function __construct () ....
}
Class Manager Extents employee{
function __construct () {
Parent::_construct ();
Echo this subclass of the parent class constructor called!;
}
}
Of course, you can also call the constructors of other classes that do not have any relation to the instance. Just precede the __construct () with the class name. Such as:
Otherclassname::__construct ();

Main family members of a class: properties, methods, constants, static members

the properties of the class:
There are two ways to assign or value the properties of a class.
1. Use public scopes to common keywords.
2, using __set () and __get () to assign values and values, the former is called the Set method (setter) or modify method (Mutator), the latter is called the Access method (accessor) or Get method (getter). It is recommended to use this method: advantages:
A, data validation can be performed uniformly in __set ().
B, to facilitate the unified management of properties.
Attention:
First: __set () and __get () only work on private properties, and for properties defined with public, they are two lazy, as follows:
Class test{
Protected $a =9, $b =2, $c;
Public $d;
function __set ($n, $v) {$this, $n = $v +2;}
function __get ($name) {return $this, $name +2;}
}
$a = new test ();
$a->b = 5; echo "
"; Echo $a->b;
The instance is only for $ A, $b, $c settings are filtered and returned by __set and __get, and for $d, it will not work. such as $a->d=5, return or 5.
Second: __set ($n, $v) takes two parameters. __get ($n) can have only one parameter. Instance:
Class test{
Private $a =5, $b =6, $c;
function __set ($n, $v)
{
if ($n ==a&& $n >0)
$this $n = $v;
Else
$this $n = $v +2;
}
function __get ($name)
{
return $this $name; If you change to return $this $name + $this->addab (); If the value of a is called, the value of A+a+b is actually returned. The default is 5+5+6=16.
}
function Addab ()
{return $this->a + $this->b;}
}
$e =new test ();
$e->a = 11; Note: The internal use of the class $this-> $n that is, the variable, but the external instance to use $e->a way.
$e->b = 12; Get 14
$e->k = 22;

The properties of the class can be freely extended, as in the above example K, whether or not with __set, when an instance is established, you can use $e->newproperty = XX; directly to create a property, but it is not recommended.

http://www.bkjia.com/PHPjc/508427.html www.bkjia.com true http://www.bkjia.com/PHPjc/508427.html techarticle One: Structure and invocation (instantiation): Class classname{}, called: $obj = new ClassName (); When a class has a constructor, it should also pass in the parameter. such as $obj = new ClassName ($v, $v 2 ...); Two: Construction ...

  • 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.