


Deserted Saturday - PHP object-oriented (3), Saturday - PHP object-oriented_PHP tutorial
Desolate Saturday-PHP object-oriented (3), Saturday-php object-oriented
hi
It’s Kaizen Saturday again. The clothes I had accumulated for two weeks were finally almost finished. Come here to learn something in the afternoon~~
1. PHP object-oriented (3)
4. Advanced Practice of OOP
4.3 Static-static members
date_default_timezone_set("PRC");
/**
* 1. The definition of a class starts with the class keyword, followed by the name of the class. Class names are usually named with the first letter of each word capitalized.
* 2. Define the attributes of the class
* 3. Define the methods of the class
* 4. Instantiate the object of the class
* 5. Use the attributes and methods of the object
*/
class Human{
public $name;
public $height;
public $weight;
public function eat($food){
echo $this-> name."'s eating ".$food."
";
}
}
class Animal{
public $kind;
public $gender;
}
class NbaPlayer extends Human{
// Definition of class attributes
public $name="Jordan"; // Define attributes
public $height="198cm";
public $weight="98kg";
public $team="Bull";
public $playerNumber="23";
private $age="44";
public $president="David Stern";
// Definition of class method
public function changePresident($newP){
$this->president=$newP;
}
public function run() {
echo "Running
";
}
public function jump(){
echo "Jumping
";
}
public function dribble(){
echo "Dribbling
" ;
}
public function shoot(){
echo "Shooting
";
}
public function dunk(){
echo "Dunking
";
}
public function pass(){
echo "Passing
";
}
public function pass getAge(){
echo $this->name."'s age is ".$this->age;
}
function __construct($name, $height, $weight , $team, $playerNumber){
print $name . ";" . $height . ";" . $weight . ";" . $team . ";" . $playerNumber."n";
$this->name = $name; // $this is a pseudo variable in php, representing the object itself
$this->height = $height; // $this can be used to set the attribute value of the object
$this->weight = $weight;
$this->team = $team;
$this->playerNumber = $playerNumber;
}
}
/**
* 1. Use the new keyword when instantiating a class into an object, followed by new followed by the name of the class and a pair of parentheses.
* 2. Using objects can perform assignment operations just like using other values
*/
$jordan = new NbaPlayer("Jordan", "198cm","98kg","Bull","23");echo "
";
$james=new NbaPlayer("James", "203cm", "120kg", "Heat", "6");echo "
";
// Visit The syntax used for the properties of an object is the -> symbol, followed by the name of the property
echo $jordan->name."
";
// Used to call a method of the object The syntax is the -> symbol, followed by the name of the method and a pair of brackets
$jordan->run();
$jordan->pass();
//The subclass calls the parent class The method
$jordan->eat("apple");
//Try to call private, directly and through the internal public function
//$jordan->age;
$ jordan->getAge();echo "
";
$jordan->changePresident("Adam Silver");
echo $jordan->president."
";
echo $james->president."< br/>";
Let’s start directly from the above example.
What you want to achieve here is, change a certain variable of the two objects at the same time. ——Use static
public static $president="David Stern";
// Definition of class method
public static function changePresident($newP){
static::$president=$newP;//Replacing static with self here is more in line with the specification
}
Pay attention to the position of static here, and the ::
in the methodThe calling method has also changed.
echo NbaPlayer::$president;echo "
";
NbaPlayer::changePresident("Adam Silver");
echo NbaPlayer::$president;echo "
";
That is, as mentioned before, a static member is a constant, so it is not targeted at a specific object (not restricted by a specific object) - Based on this, definition, assignment, and call are not Requires specific target participation.
For internal calls, use self/static::$...
External call, class name::
The purpose is data shared by all objects.
--if the variable is in the parent class when called internally
For example, in the above example, write this sentence in the parent class human
public static $aaa="dafdfa";
Then in the subclass nbaplayer, when calling the static members of the parent class,
echo parent::$aaa;
For external calls, as mentioned above, class name::, so just use the parent class name directly
echo Human::$aaa;
--Others
In a static method, you cannot access other variables, that is, you cannot use $this->
--Summary
/**
* Static members
* 1. Static properties are used to save the public data of the class
* 2. Only static properties can be accessed in static methods
* 3. Static members do not need to instantiate objects. Access
* 4. Within a class, you can access its own static members through the self or static keyword
* 5. You can access the static members of the parent class through the parent keyword
* 6. You can access it through the class name External access to static members of a class
*/
4.4 Final members
--Question
Don’t want a class to have subclasses;
Don’t want the subclass to modify a variable in the parent class (avoid overriding?)
--final
》=php5 version
Give me an example
class BaseClass{
public function test(){
echo "BaseClass::test called
";
}
public function test1(){
echo "BaseClass::test1 called
";
}
}
class ChildClass extends BaseClass{
public function test(){
echo "ChildClass::test called
";
}
}
$obj=new ChildClass();
$obj->test();
Writing the same method name in the subclass as in the parent class (the content can be different) will complete the rewriting of the parent class method!
Therefore, for methods in the parent class that you do not want to be overridden, write final
final public function test(){
And so on, for a parent class that does not want to have subclasses, write final
in the class namefinal class BaseClass{
--Summary
/**
* Rewriting and Final
* 1. Writing a method in the subclass that is exactly the same as the parent class can complete the rewriting of the parent class method
* 2. For classes that do not want to be inherited by any class, you can Add the final keyword
before class * 3. For methods that do not want to be overwritten (overwrite, modified) by subclasses, you can add the final keyword
in front of the method definition.*/
4.5 Data Access
Remove the final first
--parent
Then write
in the method in the subclassparent::test();
After running, you will find that you can still call the parent class through the parent keyword, even if it is rewritten data
--self
Then write
in the method test in the parent classself::test1();
After running, it was found that self can call data in the same class (other methods/static variables/constants)
--Summary
/**
* Data access supplement
* 1. The parent keyword can be used to call the overridden class members of the parent class
* 2. The self keyword can be used to access the member methods of the class itself, or it can be used It is used to access its own static members and class constants; it cannot be used to access the properties of the class itself; when accessing class constants, there is no need to add the $ symbol in front of the constant name
* 3. The static keyword is used to access static members defined by the class itself. When accessing static properties, you need to add the $ symbol
in front of the property name.*/
4.6 Object Interface
Very important! ! !
--Question
Different classes have the same behavior, but the same behavior has different implementation methods.
For example, both humans and animals eat, but the ways of eating are different.
--Definition
Interface defines the common behaviors of different classes, and then implements different functions in different classes.
--Chestnut
//Define an interface
interface ICanEat{
public function eat($food);
}
As you can see, there is no specific implementation of the method in the interface, but there must be a method!
So, here it is, "Humans can eat"
//Specific object, connected to the interface
class Human implements ICanEat{
public function eat($food){
echo "Human eating ".$food .".
";
}
}
$obj=new Human();
$obj->eat("shit");
Please ignore the "food" I gave.
Note that does not use extends anymore, but implements. Then, the same method name is exactly the same. Then the object must/preferably implement the method.
Continue
interface ICanEat{
public function eat($food);
}
//Specific object, connected to the interface
class Human implements ICanEat{
public function eat($food){
echo "Human eating ".$food.".
";
}
}
class Animal implements ICanEat{
public function eat($food){
echo "Animal eating ".$food.".
";
}
}
$obj=new Human();
$obj->eat("shit");
$monkey=new Animal();
$monkey->eat("banana");
Let the animals eat too!
--Reverse operation
Determine whether an object is connected to an interface.
var_dump($obj instanceof ICanEat);
will return a boolean value.
--More chestnuts
interface ICanPee extends ICanEat{
public function pee();
}
class Demon implements ICanPee{
public function pee(){
echo "Can demon pee?";
}
public function eat($food){
echo "Can demon eat ".$food;
}
}
$ghost=new Demon();
$ghost->pee();
$ghost->eat("shit");
Interfaces are essentially classes and can be inherited/inherited. However, when using an inherited interface, all methods of the parent class and "grandfather class" must have specific implementations.
--Summary
/**
* Interface
* 1. The basic concepts and basic usage of interfaces
* 2. The methods in the interface have no specific implementation
* 3. The class that implements an interface must provide the interface Defined methods
* 4. You cannot use interfaces to create objects, but you can determine whether an object implements an interface
* 5. Interfaces can inherit interfaces (interface extends interface)
* 6. In interfaces All methods defined must be public, which is a characteristic of interfaces.
*/
aaaaaaaaaaaaaa
bu xiang xie le......................
ming tian yao ge............

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

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
