I am trying to understand how Object Oriented Programming works and as an exercise I was going to try to make a kind of video store using this concept and not SQL.
The thing is that I did the code below and if not I misunderstood. I did the following,
1) I declare the class. In this case movie.
2) I declare the variables with which I am going to work.
3) I declare a constructor method so that initially these variables have a value. In my case undefined.
4) I define each movie with its respective title, year... linked to the constructor method.
5) I show the film that I want. (Later this would be done with some html input).
<?PHP
class Pelicula{
var $Titulo; // Defino las propiedades que tendrán los objetos
var $TituloOriginal;
var $Anyo;
var $Duracion;
function Pelicula(){ // Declaro constructor. Es decir, el valor de las propiedades al iniciarse
$this -> Titulo = ""; // Lo declaro como indefinido
$this -> TituloOriginal = "";
$this -> Anyo = "";
$this -> Duracion = "";
}
function IJArcaPerdida(){
$this -> Titulo = "Indiana Jones en busca del arca perdida";
$this -> TituloOriginal = "Raiders of the lost ark";
$this -> Anyo = "1981";
$this -> Duracion = "115 min";
}
function IJTemploMaldito(){
$this -> Titulo = "Indiana Jones y el templo maldito";
$this -> TituloOriginal = "Indiana Jones and the Temple of Doom";
$this -> Anyo = "1984";
$this -> Duracion = "118 min";
}
echo IJTemploMaldito();
}
?>
I can't show it and I've done tests, with echo, new... but I don't know how to do it. I know it must be fatal but I don't quite understand the topic of poo
and I've already consulted several tutorials and threads...
I tell you a couple of things
1.- The idea of the OOP is modular so it doesn't make much sense that you have to declare each movie within the class itself
2.- That last echo you put there doesn't work, because the class is a generic structure
3.- The properties in this case do not have the reserved word
var
, what they do have is their access modifier such aspublic
private
protected
4.- The methods must indicate that they are
publicos
, private orprotegidos
equally5.- You must not directly assign the equivalence of values and less use
echo
orprint
within the class since its purpose, as I say, is only to serve as a generic template; it is in the instances where you can already do it since with each new object created the values that you passed in the constructor are created6.- The constructors, at least in PHP, are
__construct
as I show you in the exampleI leave you your example adapted to some of these rules so that you can continue checking it, greetings
OOP is a fairly broad concept that you can study based on this complete guide .
One of the advantages of programming classes is that they can be shared in a collaborative environment, that is, several programmers can participate in a class (let's think of a large program in which several programmers participate).
Therefore, a fundamental rule in OOP is to respect a naming convention , and to respect a certain structure that each class should have.
We could also talk about encapsulation , polymorphism and other elements that would constitute the core of OOP.
A class generally represents a complete entity of our program, and must be able to handle it properly while respecting the fundamentals of OOP.
A
Pelicula
basic class would have the following structure:We see a few things in this class:
private
. A class should not allow its members to be modified directly.getters
andsetters
. By naming convention these methods are always calledgetMiembro
andsetMiembro
. It would be the only ways to access the members of the class, to get their values or to modify them.toString
that would return a general representation of the object.To test the class above:
If it
Anyo
were declared aspublic
in the class, you could do something like this:This would be a flagrant violation of Encapsulation , one of the basic principles of OOP.