What is the difference between Object and Class in PHP? I ask because, I don't really see the point to both of them.
Can you tell me the difference with a good example?
Object is an instance of a class. Class is a blueprint or template from which objects are created. Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc. Class is a group of similar objects.
A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.
A class is a template for creating objects in program whereas the object is an instance of a class. A class is a logical entity while object is a physical entity.
You can create instances of a class as distinct, individual objects that behave as specified. A package is simply a namespace under which you can categorize classes, analogous to a "folder" or "path", so you can group classes with related purpose or functionality.
I assume you have read the manual on basic 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.
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(); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With