<code><span><span><span><?php </span> /** * 继承,访问控制,static(静态)关键字、重写、Final关键字、数据访问补充、接口、多态、抽象类 */</span><span>/** * 1.静态属性用于保存类的公有数据 * 2.静态方法里面只能访问静态属性 * 3.静态成员不需要实例化对象就可以访问 * 4.类的内部可以通过self或者static关键字访问自身静态变量 * 5.可以通过parent关键字访问父类的静态成员 * 6.可以通过类的名称在类的定义外部访问静态成员 */</span> header(<span>"Content-Type: text/html; charset=utf-8"</span>);<span>//去除中文乱码</span> date_default_timezone_set(<span>"PRC"</span>);<span>//设置中国时区</span><span>/** * Human类定义 */</span><span><span>class</span><span>Human</span> {</span><span>public</span><span>$name</span>; <span>protected</span><span>$height</span>;<span>//本身和子类可访问</span><span>public</span><span>$weight</span>; <span>public</span><span>static</span><span>$staticValue</span>=<span>"我是Human类里的static成员。"</span>; <span>public</span><span><span>function</span><span>eat</span><span>(<span>$food</span>)</span>{</span><span>echo</span><span>$this</span>->name.<span>" is eating "</span>.<span>$food</span>.<span>"<br>"</span>; } } <span>/** * NbaPlayer类定义 */</span><span><span>class</span><span>NbaPlayer</span><span>extends</span><span>Human</span>{</span><span>//extends:表示继承,php中extends只能跟一个类的类名(单继承原则)</span><span>//属性</span><span>public</span><span>$team</span>=<span>"PTS"</span>; <span>public</span><span>$playerNum</span>=<span>"1221"</span>; <span>private</span><span>$age</span>= <span>12</span>; <span>//静态属性定义</span><span>public</span><span>static</span><span>$president</span>=<span>"David"</span>; <span>//静态方法定义</span><span>public</span><span>static</span><span><span>function</span><span>changePresident</span><span>(<span>$newPresident</span>)</span>{</span><span>static</span>::<span>$president</span>=<span>$newPresident</span>; <span>//self::$president=$newPresident;//在类定义中使用静态成员,用static或self::静态成员变量</span><span>//使用parent关键字访问父类的静态成员</span><span>echo</span><span>parent</span>::<span>$staticValue</span>.<span>"<br>"</span>; } <span>//构造函数</span><span><span>function</span><span>__construct</span><span>(<span>$name</span>,<span>$weight</span>,<span>$team</span>,<span>$playerNum</span>)</span>{</span><span>echo</span><span>"执行构造函数...<br>"</span>; <span>$this</span>->name=<span>$name</span>;<span>//this是php里面的伪变量,自身</span><span>$this</span>->weight=<span>$weight</span>; <span>$this</span>->team=<span>$team</span>; <span>$this</span>->playerNum=<span>$playerNum</span>; } <span>//析构函数</span><span><span>function</span><span>__destruct</span><span>()</span>{</span><span>echo</span><span>"执行析构函数...<br>"</span>;; } <span>//定义方法</span><span>public</span><span><span>function</span><span>run</span><span>()</span>{</span><span>echo</span><span>"Runing...\n"</span>; } <span>public</span><span><span>function</span><span>jump</span><span>()</span>{</span><span>echo</span><span>"Jumping...\n"</span>; } <span>public</span><span><span>function</span><span>dribble</span><span>()</span>{</span><span>echo</span><span>"Dribbling...\n"</span>; } <span>public</span><span><span>function</span><span>shoot</span><span>()</span>{</span><span>echo</span><span>"Shooting...\n"</span>; } <span>public</span><span><span>function</span><span>dunk</span><span>()</span>{</span><span>echo</span><span>"Dunking...\n"</span>; } <span>public</span><span><span>function</span><span>pass</span><span>()</span>{</span><span>echo</span><span>"Passing...\n"</span>; } <span>public</span><span><span>function</span><span>getAge</span><span>()</span>{</span><span>echo</span><span>$this</span>->name.<span>"今年 "</span>.<span>$this</span>->age.<span>" 岁了。<br>"</span>; } } <span>//类的实例化</span><span>$pzy</span>=<span>new</span> NbaPlayer(<span>"彭中耀"</span>,<span>"182cm"</span>,<span>"75kg"</span>,<span>"PTS"</span>,<span>"1221"</span>); <span>$pts</span>=<span>new</span> NbaPlayer(<span>"彭小耀"</span>,<span>"128cm"</span>,<span>"15kg"</span>,<span>"PTS"</span>,<span>"1221"</span>); <span>echo</span><span>$pzy</span>->name.<span>"\n"</span>; <span>echo</span><span>$pzy</span>->eat(<span>"大西瓜"</span>); <span>//echo $pzy->age;</span><span>$pzy</span>->getAge(); <span>// echo $pzy->name."的联盟总裁是".$pzy->president;//x不能访问静态成员</span><span>// echo $pts->name."的联盟总裁是".$pts->president;</span><span>//类定义外访问静态成员:类名::静态成员变量</span><span>echo</span><span>"前任是"</span>.NbaPlayer::<span>$president</span>.<span>"<br>"</span>; NbaPlayer::changePresident(<span>"彭大耀"</span>); <span>echo</span><span>"现任是"</span>.NbaPlayer::<span>$president</span>.<span>"<br>"</span>; <span>?></span></span></span></code>
Copyright Statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.
The above has introduced object-oriented PHP (1), including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment