Home > Backend Development > PHP Tutorial > PHP5 authoritative programming reading study notes with e-book download_PHP tutorial

PHP5 authoritative programming reading study notes with e-book download_PHP tutorial

WBOY
Release: 2016-07-21 15:17:39
Original
1050 people have browsed it

PHP 5 Power Programming PDF download address http://www.jb51.net/books/28207.html

In PHP4, __construct() is not used as the name of the constructor. It must be Define a method 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{
//in PHP4 Method
public function Person(){
echo "Method in PHP4";
}
//Recommended method for PHP5
public function __construct(){
echo "PHP5 Recommended method";
}
public function say(){
}
}
$p1=new Person();
?>

No value can be returned in the constructor, so the most common way to generate an error from the 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<120){
$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 to object attributes Protection is a key paradigm of OOP
Public: can be accessed anywhere
Protected: class members can be accessed by subclasses and superclasses of the class in which they are located from methods inside the object
Private: class members only It can be accessed from methods within the object by its class, but cannot be accessed from members of inherited classes. 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
{
// Public and protected can be redefined, but private but not
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 normally
echo $obj2->private; // private is not defined
echo $obj2->protected; // this The line will generate a fatal error
$obj2->printHello(); // Output Public and Protected2, but not Private

Note: All methods in the class must use keywords public, protected or private to define. If these keywords are not set, the method will be set to the default public.
Static method
Static method can be called through class name::static method without creating an object instance, or it 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;//To access static members in a class, use the self keyword
}
}
$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 an object is new, the object pointed to is returned. "Handle"
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;//will output "obj1"
?>

Since $obj1 and $obj2 point to the same block Memory area, so when you use any object to modify the value of a member variable, it will affect another object.
But sometimes, we do need to make a copy of an object (two independent memory areas). At this time, you 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 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
Recommended Method 1, the reason has been 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 "The The object is not an instance of the Rectangle class";
}
}
checkIfRectangle(new Square());//Output: Square
checkIfRectangle(new Circle());//Output: The object is not Instance of Rectangle class
?>

Note: __CLASS__ is a special constant used to store the name of the current class

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325684.htmlTechArticlePHP 5 Power Programming (PHP 5 Power Programming) PDF download address http://www.jb51.net/books /28207.html In PHP4, do not use __construct() as the name of the constructor, you must use the name of the class...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template