PHP5 authoritative programming reading study notes with e-book download_php basics
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.
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:
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:
/**
* 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.
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:
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;
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:
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
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
