一步一步学习PHP(6) 面向对象_php基础
但是我们知道,面向对象有三大特征:继承,多态和封装。
1. 继承
我们继续上一节中的例子,在PHP中,继承和Java是一样的,都使用extends关键字。
class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
}
class Student extends People
{
private $grade;
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
在这里,我们需要主要的还有我们访问父类在C# 中用base,在Java中用super,但是在PHP中,我们用parent关键字。
如果我们要访问自身的方法,那么可以用this,也可以用self。
class Student extends People
{
public function GetName()
{
return "kym";
}
private $grade;
public function SayHello()
{
echo("Good Morning,".self::GetName());
//echo("Good Morning,".$this->GetName());
}
}
2. 抽象类
提到继承,就不得不说抽象类。
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
class Student extends People
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
?>
3. 接口
接下来就是接口:
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
interface IRun
{
function Run();
}
class Student extends People implements IRun
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
public function Run()
{
echo("两条腿跑");
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
$s->Run();
?>
都没什么好说的,跟Java一模一样。
4. 构造方法
一直忘了说构造方法,其实也就是一段同样的代码:
class Person
{
private $name;
private $age;
public function Person($name,$age)
{
$this->name=$name;
$this->age=$age;
}
public function SayHello()
{
echo("Hello,My name is ".$this->name.".I'm ".$this->age);
}
}
$p=new Person("kym",22);
$p->SayHello();
?>
我们在面试中也许经常会遇到一种变态的题型,就是若干个类之间的关系,然后构造函数呀什么的调来调去。但是,在PHP中就不会遇到这样的情况了,因为在PHP中并不支持构造函数链,也就是说,在你初始化子类的时候,他不会自动去调用父类的构造方法。
class Person
{
private $name;
private $age;
public function Person($name,$age)
{
$this->name=$name;
$this->age=$age;
}
public function SayHello()
{
echo("Hello,My name is ".$this->name.".I'm ".$this->age);
}
}
class Student extends Person
{
private $score;
public function Student($name,$age,$score)
{
$this->Person($name,$age);
$this->score=$score;
}
public function Introduce()
{
parent::SayHello();
echo(".In this exam,I got ".$this->score);
}
}
$s=new Student("kym",22,120);
$s->Introduce();
?>
5. 析构函数
析构函数和C#和C++中不同,在PHP中,析构函数的名称是__destructor()。
class Student extends Person
{
private $score;
public function Student($name,$age,$score)
{
$this->Person($name,$age);
$this->score=$score;
}
public function Introduce()
{
parent::SayHello();
echo(".In this exam,I got ".$this->score);
}
function __destruct()
{
echo("我要被卸载了");
}
}
6. 多态
由于默认参数的存在,以及PHP的弱类型,使得编译时多态(也就是由于参数个数以及类型不同而造成的多态)无法实现,但是运行时多态在上文中已有提及。不再赘述。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











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

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 and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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