Classes and functions in PHP

0

I'm having some problems with php classes, to be more exact, some doubts, I do not know how classes and functions work. For example, if you wanted to create an object called an image, that has 2 functions such as Upload and Delete.

$imagen = new imagen("nombre", "formato");
$imagen->upload;
$imagen->delete;
    
asked by Esteban Fernández 28.05.2018 в 02:06
source

2 answers

2

You can do it as follows

<?php

class Imagen
{
    private $nombre;
    private $formato;

    public function __construct($nombre, $formato){
        $this->nombre = $nombre;
        $this->formato = $formato;
    }

    public function upload()
    {
        //código de alta
        $this->nombre = $_FILES['imagen']['name'];
        $nombrer = strtolower($this->nombre);
        $cd=$_FILES['imagen']['tmp_name'];
        $ruta = "img/" . $_FILES['imagen']['name'];
        $destino = "img/".$nombrer;
        $resultado = @move_uploaded_file($_FILES["imagen"]["tmp_name"], $ruta);
    }

    public function delete()
    {
        //código de eliminación
    }
}

$nuevaImagen = new Imagen("./sol.jpg", ".jpg");
$nuevaImagen->upload();
$nuevaImagen->delete();
  

The previous code understands the following, the class inside   receives a constructor that will collect two properties; both name as   format

     

The 2 later methods will be responsible for processing any of the 2   actions

    
answered by 28.05.2018 / 03:50
source
1

You can be something like:

class imagen {
    public function upload() {
      ejecuta función y retorna;
    }
    public function upload() {
      ejecuta función y retorna;
    }
}
    
answered by 28.05.2018 в 03:43