I see you got a lot of great detailled answers already, but to keep it in really simple terms:
You know what a block of code is.
You know that if you want to reuse a block of code, you put it in a function.
So far so good. Now, Classes are about organizing functions and variables.
If you create a lot of functions, you have to be really careful that their names don't clash, and you will probably want to organize your functions in separate files to keep related functions grouped together.
At its very simplest, Classes help you to group related functions into one class of functions. Instead of
add_product_to_shop();
add_product_to_cart();
add_product_to_wish_list();
you group them inside a class:
Shop::addProduct();
Cart::addProduct();
WishList::addProduct();
Here Shop
, Cart
and WishList
are Classes that each have their own addProduct()
method (function). That's a slight improvement over the other code snippet, as the function names themselves have become a lot shorter, it's obvious that the functions are not intended to work together and it's easy to add other related functions to the same Class.
What Classes further allow you though is to not only group function together, but variables too. Variables are often worse than functions when it comes to naming, as you need to be really careful not to overwrite variables. By grouping them all in their own respective Classes, you have less of a naming conflict. (A related concept to this is also called Namespaces.)
What Classes also allow you to do is to create objects from them. If the Class is the blueprint for related variables and function, you can build an "actual object" from this blueprint, even several, that act independently of each other, but behave the same. Instead of keeping track of $shopping_cart1
and $shopping_cart2
and making sure your functions act on the right variable, you just make a copy of your blueprint:
$cart = new Cart();
$cart->addProduct($x);
$anotherCart = new Cart();
$anotherCart->addProduct($y);
// $cart takes care of product $x, $anotherCart of product $y.
// No extra overhead needed to duplicate functionality
// or to make sure the addProduct() function adds to the right cart.
You don't need this very often in web development, especially PHP, but depending on your application it can be useful.
What Classes further allow you is to slightly alter them. Say you have created a great collection of function that act together as a shopping cart. Now you want to create another shopping cart that basically works the same way, but has some slight differences (say discounts should be calculated immediately). You can create a new Class and specify that it should act exactly the same as the old shopping cart, but override and alter a few specific variables or functions (this is called Inheritance).
There are a lot more possibilities that the concept of Classes enable you to do, and each of the above points entail a lot more details and specifics. Overall, Classes are used to keep things together that belong together, keep things apart that should be apart (namespacing), enable easier consistency checks (getters/setters, validating variables before changing them) and more...