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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

Solution steps for php5 not listening to port 9000: 1. Check the PHP-FPM configuration file; 2. Restart the PHP-FPM service; 3. Turn off the firewall or configure port forwarding; 4. Check whether other processes occupy port 9000.

The syntax differences between php7 and php5 are: 1. PHP7 introduces strict type declarations, while the type of PHP5 variables is implicit; 2. PHP7 introduces support for scalar type declarations, but PHP5 does not; 3. PHP7 introduces NULL Merge operator, while PHP5 checks whether a variable exists and is not null, you need to use a conditional statement; 4. PHP7 adds a new comparison operator "<=>", but PHP5 does not; 5. PHP7 introduces a new feature anonymous class , while PHP5 does not.

The differences between the version of php7 and 5 include performance improvements, scalar type declarations, return value type declarations, exception handling improvements, anonymous classes, syntax improvements, new operators, enhanced error handling and the removal of some old features. Detailed introduction: 1. Performance improvement. PHP7 introduces a new Zend engine, named Zend Engine 3.0, which brings significant performance improvement. The performance of PHP7 is approximately twice that of PHP5, mainly through improved memory management. , optimized function calls and exception handling, enhanced garbage collection, etc.

When using PHP5, some users may encounter the situation that port 9000 cannot be listened to. At this time, we need to perform some configuration and troubleshooting work to solve this problem.

If you are a website administrator, you may encounter a situation where you need to change the PHP5 port from the default port 80. This process may be a little hurried, but as long as you follow the steps below, it will be easily completed.

PHP is a widely used server-side programming language used to develop dynamic websites and applications. In recent years, the release of PHP7 has attracted some attention. PHP7 has many improvements and optimizations over previous versions (such as PHP5). In this article, we will explore the syntax differences between PHP7 and PHP5.
