Answer the question
In order to leave comments, you need to log in
What are the benefits of OOP?
He will study and understand OOP, everything is cool - like it and want it, very convenient! Here I am sitting and thinking about its practical application, where to use it?) Where do you most often use classes? It is clear that there are gurus who have the entire application in OOP, the question is where is it better for a beginner to use it?
Answer the question
In order to leave comments, you need to log in
A role object in a project is a class.
Examples of roles (using a toaster as an example): user, question, answer, comment, notification, tag, feed.
here the question is where it is better to use it for a beginner?Everywhere.
It is clear that there are gurus who have an entire application in OOPEven junior.
The profit of OOP is that there is an object and properties that you don’t need to know inside, it’s enough to know what to input and what will come out at the output, which is basically what almost all the main modern frameworks are based on - you take it and use it. It's like saying - "I shaved with a Vest, everything hurt, it's comfortable, high-quality, beautiful, but I myself can forge an ax and sharpen it sharper - it will be almost the same! What is the profit?".
There are many useful things in object-oriented programming.
For example inheritance.
// describe interfate of engine
interface IEngine {
public function getAcceleration();
public function getPower();
}
// abstract implementation of engine
abstract class Engine implements IEngine {
/**
* Engine acceleration
*/
protected $_acceleration = 0;
/**
* Engine power (h/p)
*/
protected $_power = 0;
public function getAcceleration() {
return (int) $this->_acceleration;
}
public function getPower() {
return (int) $this->_power;
}
}
// extending from engine implementation
class V12 extends Engine{
protected $_acceleration = 0.0005;
protected $_power = 300;
}
// describe interface of the car
interface ICar {
public function setEngine(IEngine $engine);
public function accelerate();
}
// abstract implementation of engine
abstract class Car {
/**
* Engine of the car
* @var IEngine
*/
protected $_engine;
/**
* Speed of the car
* @var Float
*/
protected $_speed = 0;
public function setEngine(IEngine $engine){
$this->_engine = $engine;
}
public function accelerate() {
if( ! ($this->_engine instanceof IEngine)){
throw new Exception("Wrong type of engine");
}
$this->_speed += $this->_engine->getAcceleration();
}
}
class MyCar extends Car {
public function __construct(){
$this->setEngine(new V12());
}
}
$car = new MyCar();
$car->accelerate();
OOP is a representation of real and speculative objects in the form of formalized entities.
And WEB development without OOP is often like hell. I highly recommend all the same to try to immerse yourself in the wonderful world of OOP)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question