PHP basics How to use inheritance_PHP tutorial
Inheritance is one of the three major object-oriented mechanisms. It exists in C++, JAVA, PHP. The following is an introduction to how to use it in PHP.
Usually you need classes that have the same variables and functions as other existing classes. In fact, it would be a good exercise to define a common class that is used in all projects, and to continuously enrich this class to adapt to each specific project. To make this easier, classes can extend from other classes. An extended or derived class owns all the variables and functions of its base class (this is called "inheritance", except no one dies), and includes all the parts defined in the derived class.
The elements in the class cannot be reduced, that is, any existing functions or variables cannot be unregistered. An extension class always depends on a single base class, that is, multiple inheritance is not supported. Use the keyword "extends" to extend a class.
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="keyword">class</span><span> test { </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct() { </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> name() { </span> </li> <li> <span class="vars">$this</span><span>->xname(</span><span class="string">'John'</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">private</span><span> </span><span class="keyword">function</span><span> showName(</span><span class="vars">$name</span><span>) { </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">'my name in test is '</span><span>.</span><span class="vars">$name</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="keyword">class</span><span> extendTest </span><span class="keyword">extends</span><span> test { </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct() { </span> </li> <li><span>parent::__construct(); </span></li> <li class="alt"><span>} </span></li> <li> <span class="keyword">private</span><span> </span><span class="keyword">function</span><span> showName(</span><span class="vars">$name</span><span>) { </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">'my name in extendTest is '</span><span>.</span><span class="vars">$name</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="vars">$test</span><span> = </span><span class="keyword">new</span><span> extendTest(); </span> </li> <li class="alt"> <span class="vars">$test</span><span>->name(); </span> </li> <li><span>?> </span></li> </ol>
The above example defines a class named Named_Cart, which has all the variables and functions of the Cart class, plus the additional variable $owner and an additional function set_owner() . Now, a named shopping cart is created in the normal way, and the owner of the shopping cart can be set and retrieved. The normal shopping cart class functions can still be used in the named shopping cart class:
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="vars">$ncart</span><span> = </span><span class="keyword">new</span><span> Named_Cart; </span><span class="comment">// 新建一个有名字的购物车 </span><span> </span> </li> <li class="alt"> <span class="vars">$ncart</span><span>->set_owner(</span><span class="string">"kris"</span><span>); </span><span class="comment">// 给该购物车命名 </span><span> </span> </li> <li> <span>print </span><span class="vars">$ncart</span><span>->owner; </span><span class="comment">// 输出该购物车主人的名字 </span><span> </span> </li> <li class="alt"> <span class="vars">$ncart</span><span>->add_item(</span><span class="string">"10"</span><span>, 1); </span><span class="comment">// (从购物车类中继承来的功能) </span><span> </span> </li> <li><span>?> </span></li> </ol>
This can also be called the "father-child" relationship. Create a class, the parent class, and use extends to create a new class based on the parent class: the child class. You can even use this new subclass to create another class based on this subclass.
Note:
A class can only be used after it is defined! If you need class Named_Cart to inherit class Cart, you must first define the Cart class. If you need to create another Yellow_named_cart class based on the Named_Cart class, you must first define the Named_Cart class. To put it simply: the order of class definitions is very important.
<ol class="dp-c"> <li class="alt"><span><span class="keyword">class</span><span> Person{ </span></span></li> <li> <span class="keyword">protected</span><span> </span><span class="vars">$name</span><span>;</span><span class="comment">//protected保护的权限,在子类可以访问,外部不能访问 </span><span> </span> </li> <li class="alt"> <span class="keyword">protected</span><span> </span><span class="vars">$age</span><span>; </span> </li> <li> <span class="keyword">protected</span><span> </span><span class="vars">$sex</span><span>; </span> </li> <li class="alt"> <span class="keyword">function</span><span> __construct(</span><span class="vars">$name</span><span>,</span><span class="vars">$age</span><span>,</span><span class="vars">$sex</span><span>){ </span> </li> <li> <span class="vars">$this</span><span>->name=</span><span class="vars">$name</span><span>;</span><span class="comment">//当使用this时,就算name没有声明,也会再次声明一个 </span><span> </span> </li> <li class="alt"> <span class="vars">$this</span><span>->age=</span><span class="vars">$age</span><span>; </span> </li> <li> <span class="vars">$this</span><span>->sex=</span><span class="vars">$sex</span><span>; </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"###############"</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> say(){ </span> </li> <li> <span class="func">echo</span><span> </span><span class="string">"我的名字:{$this->name},我的年龄{$this->age}:,我的性别:{$this->sex}<br>"</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">protected</span><span> </span><span class="keyword">function</span><span> eat(){ </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"wwwwwwwwwwwwwwwwwwwww<br>"</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">function</span><span> run(){ </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">protected</span><span> </span><span class="vars">$name</span><span>;</span><span class="comment">//protected保护的权限,在子类可以访问,外部不能访问 </span><span> </span> </li> <li> <span class="keyword">protected</span><span> </span><span class="vars">$age</span><span>; </span> </li> <li class="alt"> <span class="keyword">protected</span><span> </span><span class="vars">$sex</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"> <span class="comment">//继承 </span><span> </span> </li> <li> <span class="keyword">class</span><span> Student </span><span class="keyword">extends</span><span> Person{ </span> </li> <li class="alt"> <span class="keyword">var</span><span> </span><span class="vars">$school</span><span>; </span> </li> <li> <span class="keyword">function</span><span> __construct(</span><span class="vars">$name</span><span>,</span><span class="vars">$age</span><span>,</span><span class="vars">$sex</span><span>,</span><span class="vars">$school</span><span>){ </span> </li> <li class="alt"> <span>parent::__construct();</span><span class="comment">//调用父类的构造方法 </span><span> </span> </li> <li> <span class="vars">$this</span><span>->school=</span><span class="vars">$school</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="comment">//重载say()方法,进行扩展 </span><span> </span> </li> <li class="alt"> <span class="keyword">protected</span><span> </span><span class="keyword">function</span><span> say(){</span><span class="comment">//父类使用public,子类的权限不能低于父类,可以喝父类的权限相同 </span><span> </span> </li> <li> <span class="comment">//Person::say();//调用父类的say()方法 </span><span> </span> </li> <li class="alt"> <span>parent::say();</span><span class="comment">//调用父类say()方法,parent代表父类名,当父类名变化时也能够调用。 </span><span> </span> </li> <li> <span class="func">echo</span><span> </span><span class="string">"我所在的学校{$this->school}<br>"</span><span>;</span><span class="comment">//www.3ppt.com </span><span> </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">function</span><span> study(){ </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">"{$this->name}在学习<br>"</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="vars">$s</span><span>=</span><span class="keyword">new</span><span> Student(</span><span class="string">"zhangsan"</span><span>,23,</span><span class="string">"男"</span><span>); </span> </li> <li class="alt"> <span class="vars">$s</span><span>->say(); </span> </li> <li> <span class="vars">$s</span><span>->study(); </span> </li> </ol>
Advantages of inheritance:
1. One of the three major characteristics of object-oriented
2. Openness and scalability
3. Increase code reusability
4. Improve software maintainability
5. Inheritance is useful The subclass "extends" the parent class
C++ belongs to multiple inheritance, the same class can have multiple parent classes
PHP and JAVA belong to single inheritance, the same class can only have one parent class
Whether it is multiple inheritance or single inheritance, you can have multiple subclasses
As long as you have members that can be shared when designing two classes, you can separate the shared content into one The base class uses
1. Application of class inheritance
1. Declare a subclass and use the extends keyword to inherit (extend) a parent class
2. Subclasses can inherit all content from the parent class, including member attribute methods, construction methods..., which can be used in subclasses
2. Access types Control
Although subclasses can inherit everything from the parent class, private members can only be used in this class, and cannot be used in subclasses when
is encapsulated , you can not only make the inside of your own class accessible, but also make the subclasses usable, but the outside of the class cannot be used, as long as the permission is set to protected
3. Subclasses Overloading methods of the parent class
1. The subclass can declare the same method name as the parent class, that is, the subclass overrides the method of the parent class with the same name
2. The subclass method extends the parent class method
3. Call the overridden method in the parent class in the subclass
Use the parent class name::method name() parent ::Method name ()
4. Write the constructor in the subclass. If there is a constructor in the parent class, be sure to call the overridden constructor in the parent class
Note : Overloaded methods in subclasses cannot have lower access permissions than those in the parent class (subclasses can enlarge permissions, but cannot reduce permissions)
Hope this is helpful to you.

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.


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

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

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

SublimeText3 Linux new version
SublimeText3 Linux latest version
