Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are classes and objects the same thing in PHP?

Tags:

php

Just starting out with OOP in PHP and in general. From what I have been reading so far the two seem to be synonyms. Is this the case, and if not, practically speaking when people refer to objects and classes do they generally use the terms interchangeably?

like image 646
Tom Avatar asked Mar 29 '26 02:03

Tom


1 Answers

Typically one would refer to an object as an instance of a class.

So you have some class Employee.

class Employee { 
   var $name; 
   function get_name ( ) { return $this->name; } 
   function set_name ($new_name) { $this->name = $new_name; } 
 }

And you declare an instance of it like:

$assistant = new Employee();

Employee is a class. $assistant is an object, that is an instance of the Employee class.

So to answer your question - a class is not an object. You create an object when you instantiate a class.

like image 153
brendan Avatar answered Apr 02 '26 12:04

brendan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!