php中的类 对象的方法的区别,PHP中的对象和类之间的区别?

我假设你有基本的PHP OOP有

read the manual。

一个类是用来定义对象的属性,方法和行为的类。对象是你从类中创建的东西。将课程看作是一个蓝图,并将对象作为您通过蓝图(课程)构建的实际建筑物。 (是的,我知道蓝图/建筑的类比已经完成了死亡。)

// Class

class MyClass {

public $var;

// Constructor

public function __construct($var) {

echo 'Created an object of MyClass';

$this->var = $var;

}

public function show_var() {

echo $this->var;

}

}

// Make an object

$objA = new MyClass('A');

// Call an object method to show the object's property

$objA->show_var();

// Make another object and do the same

$objB = new MyClass('B');

$objB->show_var();

这里的对象是不同的(A和B),但它们都是MyClass类的对象。回到蓝图/建筑类比,将其视为使用相同的蓝图来构建两个不同的建筑物。

如果你需要一个更加文字的例子,这是另一个实际上谈论建筑物的片段:

// Class

class Building {

// Object variables/properties

private $number_of_floors = 5; // Each building has 5 floors

private $color;

// Constructor

public function __construct($paint) {

$this->color = $paint;

}

public function describe() {

printf('This building has %d floors. It is %s in color.',

$this->number_of_floors,

$this->color

);

}

}

// Build a building and paint it red

$bldgA = new Building('red');

// Build another building and paint it blue

$bldgB = new Building('blue');

// Tell us how many floors these buildings have, and their painted color

$bldgA->describe();

$bldgB->describe();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值