Home Backend Development PHP Tutorial Methods for developing large-scale PHP projects_PHP Tutorial

Methods for developing large-scale PHP projects_PHP Tutorial

Jul 13, 2016 pm 05:37 PM
object oop php exist Large object article method Purpose R&D programming For

This article introduces object-oriented programming (oop, object oriented programming) in php. I'll show you how to reduce coding and improve quality by using some OOP concepts and PHP tricks. Good luck!

Concepts of object-oriented programming:
Different authors may have different opinions, but an oop language must have the following aspects:

Abstract data types and information encapsulation
Inherit
Polymorphism

In php, encapsulation is completed through classes: --------------------------------------- ---------- class something {
// In the oop class, usually the first character is uppercase
var $x;
function setx($v) {
// Methods start with lowercase words and then use uppercase letters to separate words, such as getvalueofarea()
$this->x=$v;
}
function getx() {
return $this->x;
}
}
?>-------------------------------------------------- ----

Of course you can define it according to your own preferences, but it is best to maintain a standard, which will be more effective.

Data members are defined in the class using the "var" declaration. Before the data members are assigned a value, they have no type. A data member can
It can be an integer, an array, an associative array or an object.

Methods are defined as functions in the class. When accessing class member variables in the method, you should use $this->name, otherwise it will be a method
Legally speaking, it can only be a local variable.

Use the new operator to create an object:

​$obj=new something;

Then you can use member functions via:

​$obj->setx(5);
​$see=$obj->getx();

In this example, the setx member function assigns 5 to the object's member variable x (not the class), and then getx returns its value 5.

You can access data members through class references like: $obj->x=6. This is not a very good oop habit. I highly recommend passing
Methods are used to access member variables. If you treat member variables as unmanipulable and only use methods through object handles, you will be a
A good oop programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is allowed in PHP.

Inheritance is very easy to implement in PHP, just use the extend keyword.



-------------------------------------------------- ---

class another extends something {
var $y;
function sety($v) {
$this->y=$v;
}
function gety() {
return $this->y;
}
}

?>-------------------------------------------------- ----

The object of the "another" class currently has all the data members and methods of the parent class (something), and also adds its own data members and methods.

You can use
$obj2=new something;
$obj2->setx(6);
$obj2->sety(7);

PHP does not currently support multiple inheritance, so you cannot derive new classes from two or more classes.

You can redefine a method in a derived class. If we redefine the getx method in the "another" class, we cannot use the getx method in "something". If you declare a data member in a derived class with the same name as the base class, then when you deal with it, it will "hide" the data member of the base class.

You can define constructors in your class. The constructor is a method with the same name as the class name. It is called when you create an object of the class, for example:
-------------------------------------------------- ---

class something {
var $x;

function something($y) {
$this->x=$y;
}

function setx($v) {
$this->x=$v;
}

function getx() {
return $this->x;
}
}

?>-------------------------------------------------- ----

So you can create an object via:

​$obj=new something(6);

The constructor will automatically assign 6 to the data variable x. Constructors and methods are normal PHP functions, so you can use default parameters.

function something($x="3",$y="5")

Then:

​ ​$obj=new something(); // x=3 and y=5
​$obj=new something(8); // x=8 and y=5
​$obj=new something(8,9); // x=8 and y=9


The default parameters use the c++ method, so you cannot ignore the value of y and give x a default parameter. The parameters are assigned from left to right. If
When the parameters passed in are less than the required parameters, the default parameters will be used.

When an object of a derived class is created, only its constructor is called, and the constructor of the parent class is not called. If you want to call the base
The constructor of a class must be explicitly called in the constructor of the derived class. This can be done because all parent class methods in the derived class are
is available.

-------------------------------------------------- ---

function another() {
$this->y=5;
$this->something();
//Display calling base class constructor
}

?>-------------------------------------------------- ----

A very good mechanism of oop is to use abstract classes. Abstract classes cannot be instantiated and can only provide an interface to derived classes. Designers usually
Use abstract classes to force programmers to derive from a base class, thus ensuring that the new class contains some of the desired functionality. There is no standard way in php,
However:

If you need this feature, you can define a base class and add a "die" call after its constructor to ensure that the base class
Classes are not instantiable, and currently a "die" statement is added after each method (interface), so if a programmer does not have
in a derived class Overriding the method will throw an error. And because PHP is untyped, you may need to make sure an object is derived from your base class
class, then add a method to the base class to define the identity of the class (return some kind of identification id), and verify when you receive an object parameter
this value. Of course, if an evil programmer overrides this method in a derived class, this method will not work, but generally
The problem today is with lazy programmers, not evil programmers.

Of course, it's nice to be able to keep the base classes invisible to programmers, who can just print out the interfaces and do their job.

There is no destructor in php.
Overloading (different from overwriting) is not supported in PHP. In oop, you can overload a method to implement two or more methods with the same
name, but with a different number or type of parameters (depending on the language). PHP is a loosely typed language, so overloading via type does not
It works, but overloading with a different number of parameters doesn't work either.

Sometimes it is good to overload constructors in oop so that you can create objects in different ways (passing different numbers of parameters). in php
The trick to achieve it is:

-------------------------------------------------- ---

class myclass {
function myclass() {
$name="myclass".func_num_args();
$this->$name();

//Note that $this->name() is generally wrong, but here $name is the name of the method that will be called
}
function myclass1($x) {
code;
}
function myclass2($x,$y) {
code;
}
}

?>-------------------------------------------------- ----

Using this class is transparent to the user through additional processing in the class:

​$obj1=new myclass(1); //myclass1 will be called

​$obj2=new myclass(1,2); //myclass2 will be called


Sometimes this is very useful.

To be continued...


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486544.htmlTechArticleThis article introduces object-oriented programming (oop, object oriented programming) in php. I'll show you how to reduce coding and improve by using some oop concepts and php tricks...
Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles