-2

I am having some problem in my PHP code. I have three classes named as

  1. DatabaseObject
  2. File
  3. user_picture

And their inheritance tree from parent to child class is DatabseObect->File->user_picture. Here in user_picture I have a static property $table_name that I am using to know database table name for each class and have some common database functions in DatabaseObject class. Code for a function I am using is here

Code For DatabaseObject Class

public static function find_by_field($field="", $value="")
{
    $result_array = self::find_by_sql("SELECT * FROM ".static::$table_name." WHERE {$field} = '".$value."'"); //Line 52
    if(empty($result_array))
    {
        return false;
    }
    else
    {
        $object = static::instantiate(array_shift($result_array));
        return $object;
    }
}

Code For File Class

public $errors=array();

public $file_before_upload; // General file tyoe that has been uploaded from the form
public $id = 0;
public $size;
public $file_name;
public $file_type;
public $user_id;

protected $temp_path;
//protected static $upload_dir="";

protected $upload_errors = array(
    // http://www.php.net/manual/en/features.file-upload.errors.php
    UPLOAD_ERR_OK               => "No errors.",
    UPLOAD_ERR_INI_SIZE     => "Larger than upload_max_filesize.",
    UPLOAD_ERR_FORM_SIZE    => "Larger than form MAX_FILE_SIZE.",
    UPLOAD_ERR_PARTIAL      => "Partial upload.",
    UPLOAD_ERR_NO_FILE      => "No file.",
    UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
    UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
    UPLOAD_ERR_EXTENSION    => "File upload stopped by extension."
);
public static function get_file_by_user($id = 0){
       return DatabaseObject::find_by_field("user_id",$id); //Line 164
    }

Code for user_picture Class

protected static $table_name = "user_images";
    protected static $db_fields = array('id','file_name','file_type','size','user_id');

    public static $upload_dir ="uploads".DS."user_img";
    protected static $file_upload_limit = 15;


    public static function get_image_by_user($id = 0)
    {
        return File::get_file_by_user($id);  //Line 45
    }

And From where I am calling upper function in user_picture is

$user_image = user_picture::get_image_by_user($session->user_id);

$user_picture_path="";

if(!$user_image)
{
    $user_picture_path = $user_image::upload_dir.DS."default.png";
}
else{
    $user_picture_path = $user_image::file_path(); //Line 28
}

this code gives me the following error

Fatal error: Uncaught Error: Access to undeclared static property: DatabaseObject::$table_name in C:\xampp\htdocs\inventory-management\includes\database\DatabaseObject.php:52 Stack trace:

0 C:\xampp\htdocs\inventory-management\includes\general\File.php(164): DatabaseObject::find_by_field('user_id', '29')

1 C:\xampp\htdocs\inventory-management\includes\user\user_picture.php(45): File::get_file_by_user('29')

2 C:\xampp\htdocs\inventory-management\public\admin\layouts\admin\header\Navigation.php(28): user_picture::get_image_by_user('29')

3 C:\xampp\htdocs\inventory-management\public\admin\layouts\admin\header\admin_header.php(21): require_once('C:\xampp\htdocs...')

4 C:\xampp\htdocs\inventory-management\public\admin\add_page_category.php(44): require_once('C:\xampp\htdocs...')

5 {main} thrown in C:\xampp\htdocs\inventory-management\includes\database\DatabaseObject.php on line 52

Please if anybody can help!

Thanks,

tereško
  • 58,060
  • 25
  • 98
  • 150
kirito70
  • 65
  • 2
  • 14
  • 1
    What is in your `File.php` on line 77? – Jakub Krawczyk Feb 19 '18 at 21:02
  • How is `$user_image` declared? what are the other properties for `UserImage`? *we need more info* – William Perron Feb 19 '18 at 21:07
  • Now Please have a look I have updated it with properties and lines of errors in code comments. Thanks. – kirito70 Feb 19 '18 at 21:21
  • @JakubKrawczyk As for **User_Image** the static function **get_user_by_image($id)** return a object. – kirito70 Feb 19 '18 at 21:23
  • Instead of using static methods/variables to implement procedural code, you should attempt applying object oriented paradigm. You should also avoid breaking the encapsulation, avoid violating LSP and stop mixing the SQL and business logic. – tereško Feb 19 '18 at 22:37
  • @tereško There is not an mixing of SQL with business model as DatabaseObject is acting as DataLayer and all database related methods are implemented in it. As i have to perform some operation without using objects them here static methods come in handy. And For static Variables They are very helpful in keeping Refactoring and code reuse-ability. As for OOP Paradigm shouldn't we try and avoid repeating code for same work and Mostof libraries or builtin classes do like as in inheritance e.g: **Object->ClassA->ClassB** . Hey, Please correct where I am wrong. – kirito70 Feb 20 '18 at 04:10
  • @kirito70 there is mixing, because in your code the `DatabaseObject` is a superclass for `User_Picture` class. You can't separate layers, when you are abusing inheritance. Static variables are basically namespaced-globals. How exactly would global variables in your code improve the reusability? And, stop abusing inheritance and learn to use composition. – tereško Feb 20 '18 at 08:23

1 Answers1

2

You have to call self instead of Database:: and File::

public static function get_image_by_user($id = 0)
{
    return self::get_file_by_user($id);  //Line 45
}

public static function get_file_by_user($id = 0){
   return self::find_by_field("user_id",$id); //Line 164
}

Working example :

class A {
    static public function foo() {
        var_dump(static::$bar);
    }
}
class B extends A {
    static protected $bar = "12";

    static public function zoo(){
        self::foo();
    }
}

B::zoo();

Not working example :

class A {
    static public function foo() {
        var_dump(static::$bar);
    }
}
class B extends A {
    static protected $bar = "12";

    static public function zoo(){
        A::foo();
    }
}

B::zoo();
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • Sorry! but i was returning value it get omitted by mistake during pasting. – kirito70 Feb 19 '18 at 21:28
  • **DatabaseObject** must consider **$table_name** from **user_picture** class and use it to access data from that database table as i am using **static** keyword, but it is considering its own static property that it does not have. – kirito70 Feb 19 '18 at 21:31
  • No its there and for insert static keyword works. But, here it is not working – kirito70 Feb 19 '18 at 21:40
  • Yes it worked. Thanks. And can you kindly give me a hint about the issue.! – kirito70 Feb 19 '18 at 21:44
  • Yes, it was a question. I couldn't understand the reason about this. – kirito70 Feb 19 '18 at 21:50
  • 1
    @kirito70 I think calling `AClass::method()` doesn't refers to the inherited static object (just to a static object without references) but `self::` does. – Syscall Feb 19 '18 at 21:51
  • Ok, I got some idea. Thanks for time. It was great help. – kirito70 Feb 19 '18 at 22:00