6 votes

Show content through POO

I am trying to understand how it works, Object-Oriented Programming and as an exercise I was going to try to do a sort of video store using this concept and not SQL.

The issue is that I did the code below, and if not misunderstood. I did the following,

1) Declare the class. In this case movie.

2) Declare the variables that I'm going to work.

3) Declare a constructor method for that initially these variables have a value. In my case indefinite.

4) Define each film with their respective title, year... bound to the constructor method.

5) I Show the movie I want. (Later this would be done with some input of html).

<?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 don't get to show it and I have done tests, with echo, new... but not how to do it. Is that should be fatal but I don't understand the topic of poo and I have already consulted several tutorials and threads...

5voto

A. Cedano Points 48884

The POO is a rather broad concept that you can be studying on the basis of this comprehensive guide.

One of the benefits of programming classes is that they can be shared in a collaborative environment, that is, a class may involve multiple programmers (consider in a comprehensive program involving several developers).

For this reason, a fundamental rule in the POO is to respect a naming convention, and to respect a certain structure that should have every class.

We could also talk about the encapsulation, the poliformismo and other elements that would constitute the core of the POO.

A class generally represents a complete entity of our program, and must be able to handle it properly by respecting the fundamentals of the POO.

A class Pelicula basic would have the following structure:

   class Pelicula{
        private $Titulo;
        private $TituloOriginal;
        private $Anyo;
        private $Duracion;

       public function __construct($Titulo, $TituloOriginal, $Anyo, $Duracion){
            $this->Titulo = $Titulo;
            $this->TituloOriginal = $TituloOriginal;
            $this->Anyo = $Anyo;
            $this->Duracion = $Duracion;
       }

       public function getTitulo() {
          return $this->Titulo;
       }

       public function setTitulo($Titulo) {
          $this->Titulo=$Titulo;
       }       

       public function getTituloOriginal() {
          return $this->TituloOriginal;
       }       

       public function setTituloOriginalo($TituloOriginal) {
          $this->TituloOriginal=$titulo;
       }       

       public function getAnyo() {
          return $this->Anyo;
       }       

       public function setAnyo($Anyo) {
          $this->Anyo=$Anyo;
       }       

       public function getDuracion() {
          return $this->Duracion;
       }       

       public function setDuracion($Duracion) {
          $this->Duracion=$Duracion;
       }              

       public function toString() {
          return $this->Titulo. " ".$this->TituloOriginal." ".$this->Anyo." ".$this->Duracion;
       }              
   }

We see some things in this class:

  1. The members are declared as a private. A class should not allow their members to be modified directly.
  2. Each member has two types of accessors that are called getters and setters. By naming convention these methods are called always getMiembro and setMiembro. It would be the only ways to get access to the members of the class, to get its values or to modify them.
  3. The class would also have a method toString that would return a general representation of the object.

To test the class above:

/*Se crea una instancia del objeto*/
$unaPelicula=new Pelicula("Indiana Jons", "IJ Título Original", 2018, "2:42");

/*Obtener el Titulo, usando el getter*/
echo $unaPelicula->getTitulo();

/*Modificar el año, mediante el setter*/
$unaPelicula->setAnyo(2014);

If Anyo is declared as a public in the class, you could do something like this:

$unaPelicula->Anyo=2017;

This would be a flagrant violation of Encapsulation, one of the basic principles of the POO.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X