类和对象
<?php
//声明一个名为 Fruit 的类,它包含两个属性($name 和 $color)以及两个用于设置和获取 $name 属性的方法 set_name() 和 get_name():
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
构造函数__construct()
构造函数允许您在创建对象时初始化对象的属性。
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->get_name(); //Apple
$orange = new Fruit("Orange");
echo $orange->get_name();
?>
析构函数 __destruct()
当对象被破坏或脚本停止或退出时,会调用一个析构函数。
如果你创建了一个__destruct()
函数,PHP会在脚本结束时自动调用这个函数。
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple"); //The fruit is Apple.
?>
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function __destruct() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
$apple = new Fruit("Apple", "red"); //The fruit is Apple and the color is red.
?>
访问修饰符public/protected/private
属性和方法可以有访问修饰符来控制它们的访问位置。
/*
有三种访问修饰符:
public - 可以从任何地方访问属性或方法。 这是默认设置
protected - 属性或方法可以在类内以及从该类派生的类中访问
private - 属性或方法只能在类中访问
*/
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}
$mango = new Fruit();
$mango->name = 'Mango'; // OK
$mango->color = 'Yellow'; // ERROR
$mango->weight = '300'; // ERROR
?>
静态方法 static
<?php
class ClassName {
public static function staticMethod() {
echo "Hello World!";
}
}
?>
实例
<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
}
// Call static method
greeting::welcome();
?>
<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
public function __construct() {
self::welcome();
}
}
new greeting();
?>
静态属性 static
<?php
class ClassName {
public static $staticProp = "W3Schools";
}
?>
实例
<?php
class pi {
public static $value = 3.14159;
}
// Get static property
echo pi::$value;
?>
//一个类可以同时具有静态和非静态属性。 可以使用 self 关键字和双冒号 (::) 从同一类中的方法访问静态属性:
<?php
class pi {
public static $value=3.14159;
public function staticValue() {
return self::$value;
}
}
$pi = new pi();
echo $pi->staticValue();
?>
//要从子类调用静态属性,请在子类中使用 parent 关键字:
<?php
class pi {
public static $value=3.14159;
}
class x extends pi {
public function xStatic() {
return parent::$value;
}
}
// Get value of static property directly via child class
echo x::$value;
// or get value of static property via xStatic() method
$x = new x();
echo $x->xStatic();
?>
类常量 const
类常量在类中使用 const 关键字声明。
类常量区分大小写。 但是,建议以全部大写字母命名常量。
我们可以通过使用类名后跟范围解析运算符 (:😃 后跟常量名来从类外部访问常量,如下所示:
<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}
echo Goodbye::LEAVING_MESSAGE;
?>
//或者,我们可以通过使用 self 关键字后跟范围解析运算符 ( ::) 后跟常量名,如下所示:
<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
$goodbye->byebye();
?>
继承 extends
子类将从父类继承所有公共和受保护的属性和方法。 此外,它还可以有自己的属性和方法。
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message(); //Am I a fruit or a berry?
$strawberry->intro(); //The fruit is Strawberry and the color is red.
?>
抽象类 abstract
抽象类和方法是指父类有一个命名方法,但需要其子类来完成任务。
抽象类是包含至少一个抽象方法的类。 抽象方法是已声明但未在代码中实现的方法。
一个抽象类或方法是用abstract
关键字定义的:
<?php
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}
?>
实例
<?php
abstract class ParentClass {
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
// The child class may define optional arguments that are not in the parent's abstract method
public function prefixName($name, $separator = ".", $greet = "Dear") {
if ($name == "John Doe") {
$prefix = "Mr";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs";
} else {
$prefix = "";
}
return "{$greet} {$prefix}{$separator} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "<br>";
echo $class->prefixName("Jane Doe");
?>
接口 interface
接口允许你指定一个类应该实现什么方法。
接口使以相同方式使用各种不同的类变得容易。 当一个或多个类使用相同的接口时,称为"多态"。
接口使用 interface 关键字声明:
<?php
interface InterfaceName {
public function someMethod1();
public function someMethod2($name, $color);
public function someMethod3() : string;
}
?>
接口类似于抽象类。 接口和抽象类的区别在于:
- 接口不能有属性,而抽象类可以
- 所有接口方法必须是公共的,而抽象类方法是公共的或受保护的
- 接口中的所有方法都是抽象的,因此不能在代码中实现,也不需要
abstract
关键字 - 类可以在实现一个接口的同时从另一个类继承
使用接口 implements
要实现接口,类必须使用 implements
关键字。
实现接口的类必须实现接口的所有方法。
<?php
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
?>
实例
<?php
// Interface definition
interface Animal {
public function makeSound();
}
// Class definitions
class Cat implements Animal {
public function makeSound() {
echo " Meow ";
}
}
class Dog implements Animal {
public function makeSound() {
echo " Bark ";
}
}
class Mouse implements Animal {
public function makeSound() {
echo " Squeak ";
}
}
// Create a list of animals
$cat = new Cat();
$dog = new Dog();
$mouse = new Mouse();
$animals = array($cat, $dog, $mouse);
// Tell the animals to make a sound
foreach($animals as $animal) {
$animal->makeSound();
}
// Meow Bark Squeak
?>
特征 trait
特征用于声明可在多个类中使用的方法。 Traits 可以具有可在多个类中使用的方法和抽象方法,并且方法可以具有任何访问修饰符(public、private 或 protected)。
特征用 trait 关键字声明:
<?php
trait TraitName {
// some code...
}
?>
//要在类中使用特征,请使用 use 关键字:
<?php
class MyClass {
use TraitName;
}
?>
实例
<?php
trait message1 {
public function msg1() {
echo "OOP is fun! ";
}
}
class Welcome {
use message1;
}
$obj = new Welcome();
$obj->msg1();
?>
命名空间 namespace
命名空间是解决两个不同问题的限定符:
- 它们通过对协同工作以执行任务的类进行分组来实现更好的组织
- 它们允许将同一名称用于多个类
例如,您可能有一组描述 HTML 表格的类,例如 Table、Row 和 Cell,同时还有另一组描述家具的类,例如 Table、Chair 和 Bed。 命名空间可用于将类组织成两个不同的组,同时还可以防止两个类 Table 和 Table 混淆。
实例
<?php
namespace Html;
class Table {
public $title = "";
public $numRows = 0;
public function message() {
echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";
}
}
$table = new Table();
$table->title = "My table";
$table->numRows = 5;
?>
<!DOCTYPE html>
<html>
<body>
<?php
$table->message();
?>
</body>
</html>
可迭代对象 iterable
iterable 关键字可以用作函数参数的数据类型或函数的返回类型:
实例
//使用可迭代的函数参数:
<?php
function printIterable(iterable $myIterable) {
foreach($myIterable as $item) {
echo $item;
}
}
$arr = ["a", "b", "c"];
printIterable($arr);
?>
//返回一个可迭代对象:
<?php
function getIterable():iterable {
return ["a", "b", "c"];
}
$myIterable = getIterable();
foreach($myIterable as $item) {
echo $item;
}
?>