search
Homephp教程php手册php面向对象编程,php面向对象

php面向对象编程,php面向对象

面向对象编程的基本原则:

  • 单一职责:一个类,只需要做好一件事
  • 开放封闭:一个类,应该是可扩展的,而不是可修改的
  • 依赖倒置:一个类,不应该强依赖另一个类。每个类对应另外一个类都是可替换的
  • 配置化:尽可能地使用配置,而不是硬编码
  • 面向接口编程:只需要关心接口,不需要关心实现

 1、设置类的属性

<span>class</span><span> ShopProduct {
    </span><span>public</span> <span>$title</span> = 'default product'<span>;
    </span><span>public</span> <span>$producterMainName</span> = 'main name'<span>;
    </span><span>public</span> <span>$producterFirstName</span> = 'first name'<span>;
    </span><span>public</span> <span>$price</span> = 0<span>;
}

</span><span>$product1</span> = <span>new</span><span> ShopProduct();

</span><span>//</span><span>设置属性</span>
<span>$product1</span>->title = "My Antonia"<span>;
</span><span>$product1</span>->producterFirstName = "Cather"<span>;
</span><span>$product1</span>->producterMainName = "Willa"<span>;
</span><span>$product1</span>->price = 5.99<span>;

</span><span>//</span><span>访问</span>
<span>echo</span> 'author: '.<span>$product1</span>->producterFirstName.' '.<span>$product1</span>->producterMainName;

用以上方法设置属性值会有很多问题:

第一:PHP允许动态设置属性,如果拼错或忘记属性名时并不会得到警告。例如错误地把

<span>$product1</span>->producterMainName = "Willa";

写作

<span>$product1</span>->producterSecondName = "Willa";

,当我们输出作者名字的时候,会有意想不到的结果。

第二:类太过松散,我们没有强制设置标题、价格或者产品名称,客户端代码可以确定的是这些属性存在,但是面对的可能是默认值也可能不是,理想情况下,我们希望实例化ShopProduct对象时设置有意义的属性值。

第三:不得不重复做一些经常做的事,比如多次需要完整输出作者的名字,就要重复使用

<span>echo</span> 'author: '.<span>$product1</span>->producterFirstName.' '.<span>$product1</span>->producterMainName;

 

Statement
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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!