Fatal error: Uncaught Error: Call to undefined method image :: View ()

-1

I'm starting to study PHP, I'm trying to make the image path appear, but it returns the following error:

Fatal error: Uncaught Error: Call to undefined method image :: View () in C: \ xampp \ htdocs \ site \ home.php: 20 Stack trace: # 0 C: \ xampp \ htdocs \ app \ Pages.class.php (17): include () # 1 C: \ xampp \ htdocs \ site \ index.php (123): Pages -> construct () # 2 {main} thrown in C: \ xampp \ htdocs \ site \ home.php on line 20

Class routes:

<?php


class Rotas{

static $pastaFotos = 'fotos/';
static $siteUrl = 'http://localhost/site/';


static $index = 'index.php';
static $detalhe = '?pag=detalhe&item';
 }

Class image:

<?php


 class Imagem {


public static function Exibir($imagem, $w, $h){

    $fotos = Rotas::$siteUrl . Rotas::$pastaFotos ;

    echo $fotos;

}

     public static function Upload() {

    }

  }

home:

<?php


   $portfolio = new Conexao();



     $portfolio->ExecSQL("select * from trabalhos");


      while($ptf = $portfolio->ListarDados()){


       echo $ptf['trabalhos_titulo'] . '<br>';
        echo Imagem::Exibir('sss', '34', '34');


      }

Thank you in advance!

    
asked by anonymous 12.01.2018 / 03:29

1 answer

1

Solutions:

For you to have access to a class, you have to include it on the page where you want to call it. For this you can use one of 4 methods:

1. include and require

With these functions you can include files in your project. You just need to set the path of the file, but note: Files are included based on the path of the specified file or, if not entered, the include_path specified.

If you need to modify include_path , just use the code ini_set("include_path", "/path/to");

2. include_once and require_once

These functions are identical to the previous ones, the difference is that the PHP will check if the file has already been included in the project, if it has already been ignored.

If the file does not exist, the functions will return an error.

Using the functions in your project

In your home.php file, just include the following code:

require_once "class_image.php";

Your home.php file already has access to the Image class.

Using functions automatically

To load these files automatically, you can use the spl_autoload_register .

In this way, every time you create or instantiate an object, php will add this function to spl_autoload , check if the file exists, and include in your project (if the file exists) in>.

To do this, simply use the following code in home.php or index.php . The code will be valid for all other pages that will be added based on these files.

<?php

spl_autoload_register(function($class) {    
    $class .= ".php";    
    if (file_exists($class)) {
        require_once "classes/{$class}";
    } else {
        throw new InvalidArgumentException("File {$class} not exists.");
    }
});

Imagem::Exibir();
  

Just add the Imagem class inside the classes folder. Remember that the class name should be the same as the name of the file.

  

Tip: Always use the full path of the file.

    
12.01.2018 / 04:05