The learning _php skills of PHP5 class

Source: Internet
Author: User
Tags inheritance
Copy Code code as follows:

<?php
Class TEST
{
Const NAME = ' value '; Constant
Public $name = ' value '; Property
Public Function name ()//method
{
echo ' value ';
}
}
?>


In this context, properties and methods can also use public, protected, private three different keywords to further differentiate the scope of the properties and methods, with properties and methods of the private keyword, only the methods in the class in question can call ; properties and methods with the protected keyword, in addition to themselves, methods in their parent classes and subclasses can also be invoked, and properties and methods with the Public keyword can be invoked from objects that are instantiated, so that the greatest benefit is to add some descriptive features to all properties and methods , making it easier to organize and organize the structure of your code. The Const keyword skips first, along with the following static.

The static keyword is different from public, protected, and private (so it can be superimposed with public, protected, private):

Copy Code code as follows:

<?php
Class TEST
{
public static function name ()
{
echo ' value ';
}
}
?>

A method with the static keyword can be invoked directly through the "::" notation without instantiating the class, and with public, protected, private, or call-to-distinguish permissions, but usually with the public partner, The constant keyword const mentioned earlier should be the public static type, so you can invoke constants only in the form of Self::name,test::name, followed by __construct,__destruct, and so on. All belong to the static.

The structure of the class, the last two keywords are the abstract and final,abstract keyword means that the class must be overridden by his subclass, and the final keyword means that the class must not be overridden by his subclass, and that the two keywords function exactly the opposite. The method with abstract is an abstract method, a class with abstract methods, and an abstract class, which is described later.

Use of classes:

There are two main ways to use a class, one is to use the New keyword and the other is to use the "::" Symbol:

PHP code
Copy Code code as follows:

<?php
Class TEST
{
public static function name ()
{
echo ' value ';
}
}
Method 1: Use the New keyword
$test = new test;
$test->name ();

Method 2: Use the "::" Symbol
Test::name ();
?>


(1): Using the New keyword as an instantiation, the $test above is an object that is generated by instantiating the test class, $test->name () is called the name method that invokes the $test object.
(2): When using a class using the New keyword, you can use $this to refer to the class itself.
(3): Use "::" The premise of the symbol is that the method must be with the static keyword, when using the New keyword, the method called must have the Public keyword (a method if not with public, protected, private in any one of the keywords, The default is public)
(4): The same class can be instantiated by the new keyword into a number of different objects, but between each other is isolated; ":" The symbol in use, the method is shared between multiple uses:

PHP code
Copy Code code as follows:

<?php
Class TEST1
{
Public $name = 0;
Public Function name ()
{
$this->name = $this->name + 1;
}
}

$test 1 = new TEST1;
$test 2 = new TEST1;
$test 1->name (); $name 1 = 1
$test 2->name (); $name 1 = 1

/*--------------------------------------------*/

Class TEST2
{
public static $name = 0;
public static function name ()
{
TEST2:: $name = TEST2:: $name + 1;

}
}
Test2::name (); $name = 1
Test2::name (); $name = 2
?>
The relationship of the class:

The relationship between a class and a class, mainly abstract, interface, and inheritance:

PHP code
Copy Code code as follows:

<?php
Abstract class TEST1//Abstract
{
Abstract public Function name1 ();
Public Function name2 ()
{

}
}

Class TEST2 extends TEST1 implements TEST3//inheritance
{
Public Function name1 ()
{

}
}

Interface TEST3//interface
{
Public function name2 ();
}
?>


(1) A class with an abstract keyword is an abstract class, and the method with the abstract keyword is an abstract method, an abstract method in an abstract class that must be overwritten in a subclass.
(2) The class with the interface keyword is the interface, the interface is not allowed to implement any method, all the methods in the interface must be overwritten in the subclass.
(3) with ClassA extends CLASSB or ClassA implements CLASSB the typeface is inheritance, extends means inheriting another class, implements means inheriting another interface, only extends one class at a time, However, you can implements multiple interfaces.
(4) Abstract classes, interfaces, and methods that are ultimately inherited and implemented must be public.

During inheritance, subclasses override the parent class's method of the same name, which, if you need to call a method of the parent class in a subclass, can be invoked with the parent keyword or the class name plus the "::" Symbol:

PHP code
Copy Code code as follows:

<?php
Class TEST1 extends TEST2
{
Public Function name ()
{
Echo Parent::name2 ();
Echo Test2::name2 ();
}
}
Class TEST2
{
Public Function name2 ()
{
Echo ' value2 ';
}
}
$test = new TEST1;
$test->name ();
?>


Here to explain "::" The function of a method in a class is to invoke a constant without instantiating it (which is also understood to be static), static properties and methods, and a convenient call channel within the class by self,parent and class names.

object to the relationship between objects, mainly "= =" equals, "= = =" All equals, not equal to and clone:

PHP code
<?php
Class TEST
{
Public Function name ()
{

}
}

$test 1 = new TEST;
$test 2 = new TEST;
$test 3 = $test 1;
echo $test 1 = = $test 2? True:false; True
echo $test 1 = = $test 3? True:false; True
echo $test 2 = = $test 3? True:false; True
echo $test 1 = = $test 2? True:false; False
echo $test 1 = = $test 3? True:false; True
echo $test 2 = = $test 3? True:false; False
?>

(1) Two classes as long as the same properties and methods, is "= =" equals.
(2) Two classes must be pointed to the same object, in order to be "= = =" All equal.

Clone is special, in the above example, $test 3 = $test 1 process is not to $test 31 copies of $test 1 objects, but to $test 3 point to $test 1, if you must obtain a copy of the $test1, You must use the Clone keyword:

PHP code
Copy Code code as follows:

<?php
$test 3 = clone $test 1;
?>


Hook for class:

__autoload:
is a function name and the only hook that is used outside of the class, which is invoked when an object is instantiated without loading the class beforehand.

__construct
When a class is instantiated, the invoked hook can do some initialization.

__destruct
The hook that is called when the class is destroyed.

__call
When an object attempts to invoke a method that does not exist, the called hook

__sleep
This hook is invoked when the serialize () function is used to serialize a class.

__wakeup
This hook is invoked when an unserialize () function is used to deserialize a class.

__tostring
When an object is converted to a string, the hook is invoked (like Echo).

__set_state
This hook is invoked when the Var_export () function is invoked to manipulate a class.

__clone
This hook is invoked when the Clone keyword is used to copy operations on a class

__get
This hook is invoked when a property value in a class is obtained.

__set
This hook is invoked when setting the value of a property in a class

__isset
This hook is invoked when a property value in a class is judged using the isset () function

__unset
This hook is invoked when a property value is destroyed using the unset () function

Small Tips for classes:

When a class is instantiated, you can use this form to pass arguments to the __construct hook:

PHP code
Copy Code code as follows:

<?php
Class TEST
{
Public function __construct ($para)
{
Echo $para;
}
}

$test = new Test (' value '); Show value
?>


The foreach () function can be used to iterate over a property in a class or object, and the traversal will first be judged by the public, protected, private, and display:

PHP code
Copy Code code as follows:

<?php
Class TEST
{
Public $property 1 = ' value1 ';
Public $property 2 = ' value2 ';
Public $property 3 = ' value3 ';

Public Function name ()
{
foreach ($this as $key => $value)
{
Print "$key => $value \ n";
}
}
}

$test = new test;
foreach ($test as $key => $value)
{
Print "$key => $value \ n";
}
$test->name ();
?>

When you pass arguments to a method in a class, you can enforce the arguments, which only support the determination of the arrays and objects:

PHP code
Copy Code code as follows:

<?php     
class test1      
{     
    public function  name ( TEST2  $para  )      
    {      

    }     
}      

class test2     
{     

}     

$test 2 = new test2;     
$ test1 = new test1;     

$test 1->name (' value '); //  An error will be given because this parameter must be an object after the TEST2 instantiation      
$test 1->name ($test 1); //  will not error       
?>    


Compatible with PHP4 syntax:

PHP5 classes are PHP4-compatible, and these php4-era grammars are inherited, but are not recommended for use in PHP5 environments.

(1) using the var preset property is automatically converted to public.

(2) Use the class name as the constructor and, without the __construct construct method, look for functions that have the same class names as constructors.
Related Article

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.