22
A
回答
46
我假设你在基本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();
+4
PHP以与引用或句柄相同的方式处理对象,这意味着每个变量都包含对象引用而不是整个对象的副本+1 – kjy112 2011-01-29 14:56:06
相关问题
- 1. iPhone [(id)对象类]和object_getClassName(id对象)之间的区别
- 2. 在Java中的DTO(数据传输对象)和类对象之间的区别?
- 3. 区别对象和功能
- 4. C#中的对象和类之间有什么区别?
- 5. Perl中的对象和类之间有什么区别?
- 6. Python中的对象和类有什么区别
- 7. MustInherit和抽象类之间的区别
- 8. 获取类似对象之间的键和值的区别在PHP
- 9. 对象和字典之间的区别?
- 10. 对象和散列之间的区别?
- 11. 对象和静态方法的区别
- 12. CharSequence接口和对象类的toString()方法之间的区别
- 13. 类范围和对象范围之间的区别
- 14. 对象创建和类实例化之间的区别
- 15. 对象,*和没有类型之间的区别是什么?
- 16. PHP类和对象
- 17. 普通类的对象与泛型类的对象是否有区别?
- 18. null ==对象和对象== null之间的区别
- 19. jvisualvm活动对象和分配对象之间的区别
- 20. 对象和变量对象之间的区别
- 21. mysqltransaction对象(mysql)和transactionscope对象(asp.net)之间的区别
- 22. 子对象和包含对象之间的区别
- 23. 面向对象和非面向对象之间的区别javascript
- 24. 节点对象和元素对象的区别?
- 25. 对象和字符串对象之间的区别是什么
- 26. ,对象和指向对象的指针有什么区别?
- 27. 可变对象和不可变对象之间的区别
- 28. Kotlin:对象和同伴对象之间的区别
- 29. 对象引用声明和对象构造之间的区别?
- 30. C++中类型和类的区别?
类是必要的,PHP,因为它遵循旧的和更多的静态OOP范例。在[基于原型的语言(JavaScript,Lua)](http://en.wikipedia.org/wiki/Prototype-based_programming)中,你实际上只需要对象。所以关于课堂需要的混淆并非没有理由。 – mario 2011-01-29 15:40:49