4. How to abstract a class?
As mentioned above, the unit of object-oriented program is the object, but the object is instantiated by the class, so
The first thing we have to do is how to declare the class. It is easy to make a class. , as long as you master the basic programming grammar definition rules, you can
do it, so what is the difficulty? How many classes and objects should be used in a project? Where the class needs to be defined, define
what kind of class it is, how many objects this class instantiates, how many attributes and how many methods there are in the class. Wait, this requires readers to analyze, design and summarize practical problems in actual development.
Definition of class:
class 类名{ }Use a keyword class followed by a class name you want and a pair of braces, so that the structure of a class
is Once it’s defined, you just need to write code in it, but what should you write in it? What can I write? How to write a complete
class? As mentioned above, the purpose of using a class is to instantiate objects for us to use. This requires knowing what kind of object you want. Like what we mentioned above on an installation configuration sheet, The machine you put on is whatever it is. For example, a person is a target. How do you recommend a person you like to your leader? Of course, the more detailed the better:
First, you will introduce the person's name, gender, age, height, weight, phone number, home address, etc.
Then, you have to introduce what this person can do, whether he can drive, speak English, use a computer, etc.
As long as you introduce more, others will know more about this person. This is how we describe a person. Now let’s summarize
. All objects we use classes to describe are similar. From the above You can see from the human description that when you make a class, it is divided into two parts from the perspective of definition. The first is a static description, and the second is a dynamic description. The static description is what we call it.
Attributes, as we saw above, the person's name, gender, age, height, weight, phone number, home address, etc. Dynamically speaking, it is also the function of this human object. For example, this person can drive, speak English, can use a computer, etc. When abstracted into a program,
we write the dynamic as a function or method, function And the method is the same. Therefore, all classes are written in terms of attributes and methods. Attributes are also called member attributes of this class, and methods are called member methods of this class.
class 人{ 成员属性:姓名、性别、年龄、身高、体重、电话、家庭住址 成员方法:可以开车, 会说英语, 可以使用电脑 }
Attributes:
Declaring variables by using the keyword "var" in the class definition creates attributes of the class, although it can be given when declaring member attributes
Initial value, but it is not necessary to give initial values to member attributes when declaring a class. For example, if
is to assign a person's name to "Zhang San", then use this class instance to create dozens of people. These dozens Everyone’s name is Zhang San, so
is not necessary. We can just give the initial value of the member attribute after the object is created from the instance. For example:
var $somevar; 方法(成员函数): 通过在类定义中声明函数,即创建了类的方法。 如: function somefun(参数列表) { ... ... } <?php class Person { //下面是人的成员属性 var $name; //人的名字 var $sex; //人的性别 var $age; //人的年龄 //下面是人的成员方法 function say() //这个人可以说话的方法 { echo "这个人在说话"; }f unction run() //这个人可以走路的方法 { echo "这个人在走路"; } } ?>
The above is a declaration of a class, a class declared from attributes and methods, but it is best to declare the member attributes
Do not give an initial value, because we The person class is a description information, which can be used to instantiate objects in the future. For example, if
10 personal objects are instantiated, then each of these 10 people will have a different name, gender, and age, so in the end It is best not to assign initial values to member properties in this
place, but to assign values to each object separately.
You can use the same method to make the class you want. As long as you can use attributes and methods to describe the entity, you can define it as a class to instantiate the object.
In order to strengthen your understanding of classes, let’s make another class, a shape class. The range of shapes is a bit wider, let’s make a
rectangle. Let’s analyze it first and think about it from two aspects. Analysis, what are the properties of a rectangle? What are the functions of a rectangle?
class 矩形 { //矩形的属性 矩形的长; 矩形的宽; //矩形的方法 矩形的周长; 矩形的面积; } <?php class Rect { var $kuan; var $gao; function zhouChang() { 计算矩形的周长; }f unction mianJi() { 计算矩形的面积; } } ?>
If you use this class to create multiple rectangular objects, each rectangular object has its own length and width, and you can calculate its own perimeter
length and area.
Let’s stop here for the declaration of the class! !
The above is the content of PHP object-oriented tutorial 2. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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 makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

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 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.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

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


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment