调用类方法时未定义的变量

时间:2019-07-29 05:53:09

标签: php

调用静态方法时出现未定义的变量错误。

我是编码新手。请客气。

我正在尝试动态显示事件页面。它具有$title$price$location等,在数据库中有一个名为Onlineevent的表。

我想在此活动页面上添加一个图片库,并认为最好有一个新表(event_gallery)并包含列(idevent_id和{{ 1}})。 image_nameevent_id表中的外键。

我使用方法(Onlineevent)从数据库调用Onlineevent数据没有问题。但是,我无法调用与findy_by_id()相关的方法。请参阅代码。

event_gallery

现在,我将分享课程。

<?php 

     $event = Onlineevent::find_by_id($_GET['id']); 

    if($event){


    $event_title = $event->event_title;
    $event_type = $event->event_type;
    $event_location = $event->event_location;
    $event_date = $event->event_date;
    $event_start_time = $event->event_start_time; 
    $event_finish_time = $event->event_finish_time; 
    $max_participants = $event->max_participants; 
    $event_price = $event->event_price; 
    $event_description = $event->event_description; 
    $event_picture = $event->picture_path();
    $event_inclusion_1 = $event->inclusion_1;
    $event_inclusion_2 = $event->inclusion_2;
    $event_inclusion_3 = $event->inclusion_3;
    $event_inclusion_4 = $event->inclusion_4;
    $event_inclusion_5 = $event->inclusion_5;
    $event_inclusion_6 = $event->inclusion_6;
    $event_inclusion_7 = $event->inclusion_7;
    $event_inclusion_8 = $event->inclusion_8;

    }


 $images = Eventgallery::find_by_id($_GET['id']);

    if($images){

        $image = $images->picture_path();
    }

            echo $image;


    ?>

这会将$ _FILES ['uploaded_file']作为参数传递

class Onlineevent extends Db_object{



protected static $db_table = "onlineevent";
protected static $db_table_fields = array('event_type','event_title','event_picture', 'event_location','event_date','event_start_time','event_finish_time','max_participants','event_price','event_description','event_map','inclusion_1','inclusion_2','inclusion_3','inclusion_4','inclusion_5','inclusion_6','inclusion_7','inclusion_8','inclusion_9','inclusion_10');

public $id;
public $event_type;
public $event_title;
public $event_picture;
public $event_location;
public $event_date;
public $event_start_time;
public $event_finish_time;
public $event_koreans;
public $max_participants;
public $event_foreigners;
public $event_price;
public $event_description;
public $event_map;
public $inclusion_1;
public $inclusion_2;
public $inclusion_3;
public $inclusion_4;
public $inclusion_5;
public $inclusion_6;
public $inclusion_7;
public $inclusion_8;
public $inclusion_9;
public $inclusion_10;



public $tmp_path;
public $upload_directory = "images";
public $errors = array();

public $upload_errors_array = array(
0 => 'There is no error, the file uploaded with success',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',

);

这是二等班

public function set_file($file) {

    if(empty($file) || !$file || !is_array($file)) {

        $this->errors[] = "There was no file uploaded here";
        return false; 

    } elseif($file['error'] !=0){

        $this->error[] = $this->upload_errors_array[$file['error']];
        return false;
    } else {

    $this->event_picture = basename($file['name']);
    $this->tmp_path = $file['tmp_name'];
    $this->type     = $file['type'];
    $this->size     = $file['size'];




    }



}

public function picture_path(){

    return $this->upload_directory.DS.$this->event_picture;
}



public function save(){

    if($this->id){

        $this->update();

    } else {

        if(!empty($this->errors)){

            return false;
        }

        if(empty($this->event_picture) || empty($this->tmp_path)){
            $this->errors[] = "the file was not available";
            return false;
        }

        $target_path = SITE_ROOT .DS. 'admin'.DS. $this->upload_directory. DS . $this->event_picture;

        if(move_uploaded_file($this->tmp_path, $target_path)){
            if($this->create()){

                unset($this->tmp_path);

                return true;
            }

        } else {

            $this->errors[] = "the folder probably does have permission";
            return false; 
        }


    }


}

这会将$ _FILES ['uploaded_file']作为参数传递

<?php

class Eventgallery extends Db_object{



protected static $db_table = "event_gallery";
protected static $db_table_fields = array('event_id','image_name');

public $id;
public $event_id;
public $image_name;


public $tmp_path;
public $upload_directory = "images";
public $errors = array();
//    public $allowTypes = array('jpg','png','jpeg','gif');

public $upload_errors_array = array(
0 => 'There is no error, the file uploaded with success',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',

);

我希望能够调用静态函数public function set_file($file) { if(empty($file) || !$file || !is_array($file)) { $this->errors[] = "There was no file uploaded here"; return false; } elseif($file['error'] !=0){ $this->error[] = $this->upload_errors_array[$file['error']]; return false; } else { $this->image_name = basename($file['name']); $this->tmp_path = $file['tmp_name']; $this->type = $file['type']; $this->size = $file['size']; } } public function picture_path(){ return $this->upload_directory.DS.$this->image_name; } public function save(){ if($this->id){ $this->update(); } else { if(!empty($this->errors)){ return false; } if(empty($this->image_name) || empty($this->tmp_path)){ $this->errors[] = "the file was not available"; return false; } $target_path = SITE_ROOT .DS. 'admin'.DS. $this->upload_directory. DS . $this->image_name; if(move_uploaded_file($this->tmp_path, $target_path)){ if($this->create()){ unset($this->tmp_path); return true; } } else { $this->errors[] = "the folder probably does have permission"; return false; } } } ,以便可以访问数据,然后将其打印到事件页面上。

谢谢

1 个答案:

答案 0 :(得分:0)

if语句失败时,未定义$image变量。

if ($images) {
    $image = $images->picture_path();
}

echo $image;

您可以先声明来解决此问题。

$image = '';

if ($images) {
    $image = $images->picture_path();
}

echo $image;