search
HomeBackend DevelopmentPHP TutorialPHP5 authoritative programming reading study notes with e-book download_PHP tutorial

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$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...
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
How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!