Home >php教程 >php手册 >PHP5 authoritative programming reading study notes with e-book download_php basics

PHP5 authoritative programming reading study notes with e-book download_php basics

WBOY
WBOYOriginal
2016-05-16 09:00:262175browse

PHP 5 Power Programming PDF download address /books/28207.html

Not used in PHP4 __construct() As the name of the constructor, a method must be defined using the name of the class, just like in C.
In PHP5, a new unified constructor naming method is used: __construct(). Of course, it is also possible to use class names.
However, if you use both at the same time, the system will use __construct() by default.

Copy code The code is as follows:

class Person{
//Methods in PHP4
public function Person(){
echo "Methods in PHP4";
}
//Methods recommended in PHP5
public function __construct(){
echo "Recommended method for PHP5";
}
public function say(){
}
}
$p1=new Person();
?>

You cannot return a value in a constructor, so the most common way to generate an error from within a constructor is to throw an exception.
The code is as follows:
Copy code The code is as follows:

class Person{
private $_age;
public function __construct($age){
try {
if ($age$this->_age=$age;
}else {
throw new Exception("The age you entered is too old");
}
}catch (Exception $e){
echo $e->getMessage();
}
}
}
$p1=new Person(121);
?>

Access Control
Access protection of object attributes is a key paradigm of OOP
Public: can be accessed anywhere
Protected: class members can be accessed from methods inside the object by subclasses and parent classes of the class in which they are located
Private: Class members can only be accessed by methods within the object of the class in which they are located, but cannot be accessed from members of the inherited class. Because private members will not be inherited, two related classes can declare a private variable with the same name.
That is, both classes can only see their own private properties, and there is no relationship between private members.
Example:
Copy code The code is as follows:

/**
* Define MyClass
*/
class MyClass
{
public $ public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public; echo $this->protected;
echo $this->private;
}
}
$obj = new MyClass();
echo $obj->public; // This line can be executed normally
echo $obj->protected; // This line will generate a fatal error
echo $obj->private; // This line will also generate a fatal error
$obj ->printHello(); // Output Public, Protected and Private
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// Can be used for public Redefine with protected, but private instead of
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this-> protected;
echo $this->private;
}
}
$obj2 = new MyClass2();
echo $obj->public; // This line can be executed normallyecho $obj2->private; // private is not defined
echo $obj2->protected; // This line will generate a fatal error
$obj2->printHello(); // Output Public, Protected2 , but Private

Note: All methods in the class must be defined using the keywords public, protected or private. If these keywords are not set, the method will be set to the default public.
Static method
Static methods can be called through the class name::static method without creating an object instance, or they can be called in an object instance through $this->static method or self: :Static method to call.
Copy code The code is as follows:

class Foo
{
public static $my_static = 'foo';
public static function staticValue() {
return self::$my_static;//Use the self keyword to access static members in a class
} }
$obj=new Foo();
echo $obj->staticValue();//Method 1
echo Foo::staticValue();//Method 2
?>

Clone object
In PHP4, when new an object is returned, the "object itself" is returned.
In PHP5, when new an object is time, what is returned is "a handle pointing to the object"
This means that in PHP5, when assigning an object instance ($obj1) to another variable ($obj2), both objects point to the same memory area. .
For example:
Copy code The code is as follows:

class test{
public $str;
}
$obj1=new test();
$obj1->str="obj1";
$obj2= $obj1;
$obj2->str="obj2";
echo $obj1->str;//"obj1" will be output
?>

Since $obj1 and $obj2 point to the same memory area, when using any object to modify the value of a member variable, it will affect the other object.
But sometimes, we do need to make a copy of an object (two independent memory areas). At this time, we can use the language command clone
Refer to the example below;
Copy code The code is as follows:

class test{
public $str;
}
$obj1=new test();
$obj1->str="obj1";
$obj2= clone $obj1;
$obj2->str="obj2";
echo $obj1->str;//"obj2" will be output
?>

parent:: and self::
self:: points to The current class, and is usually used to access static members, methods and constants
parent:: points to the parent class, and it is often used to call the constructor and methods of the parent class, and can also be used to access the members and constants of the parent class.
Note: You should use parent:: instead of a specific name of the parent class, because this allows you to easily change the hierarchy of your class.
Example:
Copy code The code is as follows:

class Father{
public function __construct(){
echo "Call the constructor of the parent class
";
}
}
class Son extends Father {
public function __construct (){
parent::__construct();//Method 1
// Father::__construct();//Method 2
echo "Call the constructor of the subclass";
}
}
$son=new Son();
?>

Result:
Call the constructor of the parent class
Call the constructor of the subclass
It is recommended to use method 1 for the reasons mentioned above.
instanceof instance
Copy code The code is as follows:

class Rectangle {
public $name=__CLASS__;
}
class Square extends Rectangle {
public $name=__CLASS__;
}
class Circle{
public $name=__CLASS__;
}
function checkIfRectangle($shape){
if ($shape instanceof Rectangle ){
echo $shape->name;
}else {
echo "This object is not an instance of the Rectangle class";
}
}
checkIfRectangle(new Square());//Output: Square
checkIfRectangle(new Circle());/ /Output: The object is not an instance of the Rectangle class
?>

Note: __CLASS__ is a special constant used to store the name of the current class
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn