Error: Call to a member function get Startdate () on null

1

I'm having problems with this error and I can not tell why it is.

The error is as follows:

On the one hand I have a form which sends various data per POST , including a date. I receive the information in another PHP file where I process other data but I do not change the date. Then I create the object articulo , which among other things has the date, and I call the function altaArticulo , which makes an INSERT.

Inside the function I use the functions get to obtain the data of the object. All data arrive well minus the date which is null .

This is my code:

$fecha_inicio = strip_tags($_POST['fecha_inicio']); //dd-mm-yy     
$fecha_inicio = date("Y-m-d", strtotime($fecha_inicio));
// Usando die(var_dump($fech_inicio) acá la variable tiene la fecha guardada correctamente

//Creo el objeto publicacion    
$p = new art_pub('',$id_articulo_ingresado,$id_usu,$fecha_inicio,$fecha_fin,$tipo_publicacion);

$q = $p->altaPublicacion($conex);

Function of registration in the class file:

public function altaPublicacion($conex){
    $id_art=$this->getIdArt();
    $id_usu=$this->getIdUsu();
    $fecha_inicio->getFechaInicio();// En esta linea da el error
    $fecha_fin->getFechaFin();
    $tipo->getTipoVenta();


    $sql = "INSERT INTO 'publica' ('fecha_in','fecha_fin','tipo','id_u','id_a')
    VALUES (:fecha_inicio, :fecha_fin, :tipo, :id_usu, :id_art)";

    $result = $conex->prepare($sql);
    $result->execute(array(':fecha_inicio'=>$fecha_inicio, ':fecha_fin'=>$fecha_fin,':tipo'=>$tipo
    , ':id_usu'=>$id_usu, ':id_art'=>$id_art));

    // Guardo el id de la publicacion luego de insertar para redirigir a la publicacion finalizada
    $id_publicacion = $conex->lastInsertedId();

    return ($id_publicacion);
}

I do not realize because he does not receive the data. Why can it be and how can I solve it?

    
asked by Manuel Gancio 19.06.2017 в 19:54
source

1 answer

0

If getFechaInicio() is a method of the class and $fecha_inicio is declared as private in the same class. To access them you must use the reserved word $this .

For example:

$this->fecha_inicio = $this->getFechaInicio();

But, if the getFechaInicio() method is declared as static (static), then to call it you would have to put:

$this->fecha_inicio = self::getFechaInicio();

Note :

If so, to respect the OOP standards, that method should be a setter , not a getter ..., calling it something like setFechaInicio() . That is, in a class the variables declared as private can not be modified from outside it, that is called encapsulation .

If your method getFechaInicio() were public, you could modify from outside the class the value of a private member of the same, therefore, that class would not fulfill an essential principle of the OOP You should then have a setter method, to modify your value from within and a getter method to get that value from outside strong>. Something like this:

//getter: no puede modificar el valor
public function getFechaInicio() 
{
     return $this->fecha-inicio;
}


//setter: sí puede modificar el valor, pero desde dentro xq es private
private function setFechaInicio($valor) 
{
   $this->fecha_inicio = $valor;     
}

By naming convention , that kind of methods always start with the English words get... set... , followed by what they getean or set. You can call them as you want, but in programming it is important to respect the naming convention .

    
answered by 19.06.2017 / 22:59
source