Clase Departamento

<?php

/**
 * Description of Departamento
 *
 * @author incid
 */
class Departamento {
    private $nombre;
    private $gerente;
    private $num_empleados;
    /**
     * Esta función nos construye el objeto
     * Es muy bonita y está muy bien hecha
     * Saludos a mi clase que me está leyendo
     * 
     * @param string $nombre El nombre del departamento
     * @param string $gerente El gerente asignado
     * @param int $num_empleados Número de empleados
     */
    function __construct($nombre,$gerente,$num_empleados=2) {
        $this->setNombre($nombre);
        $this->setGerente($gerente);
        $this->setNum_empleados($num_empleados);
    }
    function getNombre() {
        return $this->nombre;
    }

    function getGerente() {
        return $this->gerente;
    }

    function getNum_empleados() {
        return $this->num_empleados;
    }

    function setNombre($nombre) {
        if(empty($nombre)){
            throw new Exception("El nombre no puede estar vacío");
        }
        $this->nombre = $nombre;
    }

    function setGerente($gerente) {
        if(empty($gerente)){
            throw new Exception("El gerente no puede estar vacío");
        }
        $this->gerente = $gerente;
    }

    function setNum_empleados($num_empleados) {
        if($num_empleados<1){
            throw new Exception("Tiene que tener al menos un empleado");
        }
        $this->num_empleados = $num_empleados;
    }
    
    function __toString() {
        return $this->getNombre()." - ".$this->getGerente()." - ".$this->getNum_empleados();
    }
     
     
}

$comercial=new Departamento("Comercial","Eva Pi");

$cad="Departamento: ".$comercial;

echo $cad;

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos