Fatal error: Uncaught Error: Call to Undefined method Image::Display()

Asked

Viewed 1,342 times

-1

I’m starting to study PHP, I’m trying to make the image path appear, but 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

Route class:

<?php


class Rotas{

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


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

Picture class:

<?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');


      }

Thanks in advance!

1 answer

1


Solutions:

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

1. include and require

With these functions you can include files in your project. You only need to define the file path, but attention: Files are included based on the path of the reported file or, if not informed, the specified include_path.

If you need to modify the 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 being that the PHP will check whether the file has already been included in the project, if it has already been included, ignore.

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


Using the functions in your project

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

require_once "class_image.php";

Ready, your file home php. already has access to the class Image.


Using the functions automatically

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

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

To do this just use the following code in the home php. or index php.. The code will apply to all other pages that may 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 class Imagem inside the briefcase classes. Remembering that the class name should be the same as the file name.

Tip: Always use the full file path.

  • Thank you very much

  • Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.