SoFunction
Updated on 2025-03-10

My php study notes (graduation design)

The php syntax is simple, the application is very good, and the class library is powerful, and it can indeed write a very powerful server-side. For someone like me who only needs small functional servers, it's a great deal.
To simply study php, I think it is better to read the manual. I spent a few days looking at the syntax, because I had a programming foundation, so it looked faster now. I just finished writing a simple server in PHP, of course, it is intended to support a client for ticket booking system. Below are my notes on the learning process. There will be a review in the future.
When an object of a certain class does not exist, the method in a certain class can be called through the scope distinguisher (::);
When accessing methods in the base class, you can write them as parent::method();
serialize() returns a string containing a byte stream representation of any value that can be stored in PHP.
unserialize() This string can be used to reconstruct the original variable value.
Serialization to save an object can save all variables in an object. Functions in the object will not be saved, only the class name.
When serializing and deserializing the same object, you can use the definition file method containing the same object to implement it.
This is because "new" does not return a reference by default, but returns a copy.
php5
Characteristics of classes and objects:
visibility: visibility
Access limit for attributes: public: This attribute can be accessed anywhere.
protect derived class or parent class can access this property, or any item in the class that defines this property)
Private: Only internal class can access
A member declared as static can not be accessed with
an instantiated class object (though a static method can).
Static members and methods cannot be re-defined in subclasses.
(If a member is defined as static, then the member cannot be accessed by the instantiated object.
Static members cannot be redefined in subclasses).
Static definitions must be after accessing properties, such as: protect static
Static methods can not be called instantiated, so static methods cannot be used with $this parameter.
Static members cannot be accessed with ->.
constant: Constant keyword, const is used to define unchangeable constants, and the $ symbol is not required when defining.
The definition method is generally: const aconstant = 'constant';
The variables defined by gloabl in php are used throughout the page, including the pages contained in require and the pages contained in include.
Abstract class:
Abstract classes cannot be instantiated, and any class with abstract methods must be defined as abstract classes.
If you inherit the abstract class, any abstract method in the abstract class must be rewrite and implemented. These methods have only access limits
The access limit is the same as the method of the abstract parent class or lower.
Both abstract classes and abstract methods use abstract as keywords.
Object interface
The object interface allows you to specify which methods must be implemented, rather than letting you define which methods are captured.
The object interface is defined using the interface keyword. It is a standard class, but none of its methods are implemented.
Any method in an interface object must be public, which is what the interface object must follow.
Implementing an interface must be marked with implements, so the implementation of the interface method must be in a class. A class can implement multiple interfaces.
Overload:
Iterator:
The iterator can access all public object members in the class.
Implement the iterator interface in PHP5, which allows you to define how objects are accessed iteratively.
Design Pattern:
Design patterns provide a good framework to implement some functional organization.
Factory mode: Instantiate a required object during operation.
Single-interest mode: The most obvious example is: database connection objects. Here is a best example of a singleton pattern:
Singleton Function
Copy the codeThe code is as follows:

<?php
class Example
{
// Hold an instance of the class
private static $instance;
// A private constructor; prevents direct creation of object
private function __construct()
{
echo 'I am constructed';
}
// The singleton method
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
// Example method
public function bark()
{
echo 'Woof!';
}
// Prevent users to clone the instance
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}

You can also implement the iteratoraggregate interface object in php5 to define your own iterative method.
Magic function:
The function names __construct, __destruct (see Constructors and Destructors),
__call, __get, __set, __isset, __unset (see Overloading), __sleep, __wakeup,
__toString, __clone and __autoload are magical in PHP classes.
These functions exist in every php class. You don't use __ to define functions at will, unless you really want this function to have magic.
__tostring() function, this function will determine what will happen when an object is converted into a character.
Final keyword:
The final keyword is used to prevent the class or method declared by the final keyword from being inherited and overwritten.
Parameter type mandatory:
You can prefix the parameter name to control the parameter type passed.
require() and include() are exactly the same in all aspects except how to deal with failures.
include() generates a warning and require() results in a fatal error.
In other words, if you want to stop processing the page when you lose a file, don't hesitate and use require() .
The require_once() statement contains and runs the specified file during script execution.
This behavior is similar to the require() statement,
The only difference is if the code in the file has been included,
It will not be included again. See the documentation for require() on how this statement works.
PHP has a type operator: instanceof. instanceof is used to determine whether a given object comes from the specified object class.
Code example:
Copy the codeThe code is as follows:

<?php
class A { }
class B { }
$thing = new A;
if ($thing instanceof A) {
echo 'A';
}
if ($thing instanceof B) {
echo 'B';
}
?>

The end tag of PHP code snippet can be avoided, in some cases when using output buffering and
It would be better to omit include() or require() .
include() is not the case, the script will continue to run. At the same time, you must also confirm that the appropriate include_path is set.
__CLASS__: refers to the current class.
Exception handling, extend exception handling class exception as needed
The require() statement contains and runs the specified file;