PHP中对象和类的区别?

时间:2021-03-20 13:28:51

What is the difference between Object and Class in PHP? I ask because, I don't really see the point to both of them.

PHP中对象和类的区别是什么?我这样问是因为,我并没有真正理解这两个问题的意义。

Can you tell me the difference with a good example?

你能给我举个好例子吗?

2 个解决方案

#1


48  

I assume you have read the manual on basic PHP OOP.

我假设您已经阅读了关于基本PHP OOP的手册。

A class is what you use to define the properties, methods and behavior of objects. Objects are the things you create out of a class. Think of a class as a blueprint, and an object as the actual building you build by following the blueprint (class). (Yes, I know the blueprint/building analogy has been done to death.)

类是用来定义对象的属性、方法和行为的类。对象是从类中创建的。将类视为蓝图,将对象视为按照蓝图(类)构建的实际构建。(是的,我知道蓝图/建筑的类比已经被用死了。)

// 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();

The objects here are distinct (A and B), but they are both objects of the MyClass class. Going back to the blueprint/building analogy, think of it as using the same blueprint to build two different buildings.

这里的对象是不同的(A和B),但它们都是MyClass类的对象。回到蓝图/建筑的类比,把它想象成使用相同的蓝图建造两个不同的建筑。

Here's another snippet that actually talks about buildings if you need a more literal example:

如果你需要一个更真实的例子,这里有另一个关于建筑物的片段:

// 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();

#2


0  

For the new developers:

为新开发人员:

Class

class is a collection of method and variables

类是方法和变量的集合

class Test{

  const t = "OK";
  var $Test;
  function TestFunction(){

  }
}

Object

对象

Obeject is a instance of a class (when you want to use your class and the thing you create)

Obeject是类的一个实例(当你想使用你的类和你创建的东西时)

$test = new Test();
$test->TestFunction();//so here you can call to your class' function through the instance(Object)

#1


48  

I assume you have read the manual on basic PHP OOP.

我假设您已经阅读了关于基本PHP OOP的手册。

A class is what you use to define the properties, methods and behavior of objects. Objects are the things you create out of a class. Think of a class as a blueprint, and an object as the actual building you build by following the blueprint (class). (Yes, I know the blueprint/building analogy has been done to death.)

类是用来定义对象的属性、方法和行为的类。对象是从类中创建的。将类视为蓝图,将对象视为按照蓝图(类)构建的实际构建。(是的,我知道蓝图/建筑的类比已经被用死了。)

// 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();

The objects here are distinct (A and B), but they are both objects of the MyClass class. Going back to the blueprint/building analogy, think of it as using the same blueprint to build two different buildings.

这里的对象是不同的(A和B),但它们都是MyClass类的对象。回到蓝图/建筑的类比,把它想象成使用相同的蓝图建造两个不同的建筑。

Here's another snippet that actually talks about buildings if you need a more literal example:

如果你需要一个更真实的例子,这里有另一个关于建筑物的片段:

// 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();

#2


0  

For the new developers:

为新开发人员:

Class

class is a collection of method and variables

类是方法和变量的集合

class Test{

  const t = "OK";
  var $Test;
  function TestFunction(){

  }
}

Object

对象

Obeject is a instance of a class (when you want to use your class and the thing you create)

Obeject是类的一个实例(当你想使用你的类和你创建的东西时)

$test = new Test();
$test->TestFunction();//so here you can call to your class' function through the instance(Object)