Show content through POO


I'm 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 question is that I made the code below and if I did not understand wrong. I did the following,

1) I declare the class. In this case film.

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 indefinite case.

4) I already define each film with its respective title, year ... linked to the constructor method.

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


    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 not show it and I've done tests, with echo, new ... but I do not know how it's done. I know it must be fatal but I do not finish understanding the issue of poo and I have already consulted several tutorials and threads ...

asked by NEA 04.03.2018 в 22:54

2 answers


POO 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, a class can involve several programmers (think of a broad program involving several programmers).

Therefore, a fundamental rule in the OOP is respect a naming convention , and 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 usually represents a complete entity of our program, and should be able to handle it properly respecting the fundamentals of the OOP.

A class% co_of 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) {

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

       public function setTituloOriginalo($TituloOriginal) {

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

       public function setAnyo($Anyo) {

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

       public function setDuracion($Duracion) {

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

We see some things in this class:

  • Members are declared as Pelicula . A class should not allow its members to be modified directly.
  • Each member has two types of accessors that are called private and getters . By name convention these methods are always called setters and getMiembro . It would be the only ways to access the members of the class, to obtain their values or to modify them.
  • The class would also have a setMiembro method 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*/

    If toString were declared as Anyo in the class, you could do something like this:


    We would be facing a flagrant violation of Encapsulation , one of the basic principles of OOP.

    answered by 05.03.2018 / 02:15

    I'll tell you a couple of things

    1.- The idea of the OOP is modular, so it does not make much sense that you have to declare each movie within the class itself.
    2. That last echo that you put there does not go, because the class is a generic structure
    3.- The properties in this case do not have the reserved word var , what if they have is their access modifier as it can be   public
    4.- The methods must indicate that they are publicos , private or protegidos equally | 5.- You must not directly assign the equivalence of values and less use echo or print within the class since its purpose as I say is only to serve as a generic template; 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 created
    6.- The constructors at least in PHP are with __construct as I show you in the example

    I leave your example adapted to some of these rules for you to keep checking, greetings

        class Pelicula{
            // Defino las propiedades que tendrán los objetos
            public $Titulo;        
            public $TituloOriginal;
            public $Anio;
            public $Duracion;
            public function __construct($Titulo, $TituloOriginal, $Anio, $Duracion)
                $this->Titulo = $Titulo;
                $this->TituloOriginal = $TituloOriginal;
                $this->Anio = $Anyo;
                $this->Duracion = $Duracion;
            public function nuevaPelicula(){
                return  $this->Titulo.$this->TituloOriginal.$this->Anio.$this->Duracion;
        $obj = new Pelicula("titulo", "titulote", "2018", "123");
        echo $obj->nuevaPelicula();
    //si necesitas declarar los valores de otra película, entonces creas una instancia nueva
        $obj2 = new Pelicula("titulo2", "titulito", "2017", "249");
        echo $obj2->nuevaPelicula();
    answered by 04.03.2018 в 23:14