Is this class correct?

6

I'm new to PHP, an instructor did this Controller class, it's an MVC, and he put __Hospital(){} without using it, is that necessary?

class Hospital{
    private $cod;
    private $nom;
    private $dir;

    public function __Hospital() {

    }

    public function _Hospital($cod, $nom, $dir) {
        $this->cod = $cod;
        $this->nom = $nom;
        $this->dir = $dir;
    }

    public function getCod() {
        return $this->cod;
    }

    public function getNom() {
        return $this->nom;
    }

    public function getDir() {
        return $this->dir;
    }

    public function setCod($cod) {
        $this->cod = $cod;
    }

    public function setNom($nom) {
        $this->nom = $nom;
    }

    public function setDir($dir) {
        $this->dir = $dir;
    }
}
    
asked by Francis Yourem 11.04.2018 в 20:30
source

1 answer

9

The central idea of constructors as magic methods in PHP specific; is to initialize the value of the properties to identify or belong to a class.

EXAMPLE

<?php

class Padre{
   protected $name;
   protected $edad;

  public function __construct($name, $edad){
    $this->name = $name;
    $this->edad = $edad;
  }
}

Now it is not so much that it is incorrect or not, but rather how the PHP interpreter will read it;

Quote:

  

As of PHP 5.3.3, methods with the same name as the last   element of a class in a name of spaces will no longer be treated   as a constructor. This change does not affect classes without space   names.

Example of the appointment

<?php
namespace Foo;
class Bar {
    public function Bar() {
        // Tratado como constructor en PHP 5.3.0 - 5.3.2
        // Tratado como método regular a partir de PHP 5.3.3
    }
}

Source of inquiry

This way of declaring a constructor where it has the same name of the class, is common for example although not limited to languages like Java; where the following is valid

public class Human
{
  public Human(type param1, type param2, ...)
  {
  }
  ......
}

Where in fact if you omit it you will mark an error when trying to compile the project similar to: invalid method declaration

    
answered by 11.04.2018 / 20:55
source