Search code examples
late-static-binding

using late staic binding variable with another class


I have created a class for writing sql query in which i have used late static binding concept and i am trying to call its insert method in different class to insert the values here is sqlQuery class

  class sqlQuery
{
    public static $table=" table 1 ";
    public static $colum1="  colum1  ";
    public static $colum2=" colum2  ";
    public static $colum3=" colum3  ";
    public static $colum4=" colum4  ";
    public $value1=" value1 ";
    public $value2="  value2   ";



   public function insert( $value1,$value2)
    {
    echo "INSERT INTO" .static::$table ."(" . static::$colum1 .' , ' .static ::$colum2. ")  VALUES('$value1' , '$value2')" ;        
    }

}

And this is my second class file where i am using insert method from first class what i am trying to do, get the table name and Column from this class using late static binding....please help how can i do this...here is my 2nd class file

  class gallery extends logo 
{
    public $object;

    public static $colum1=" status ";
    public static $colum2=" order ";
    public static $colum3=" colum3  ";
    public static $colum4=" colum4  ";

   function __construct()
     {
        parent::__construct();
        //$this->object=new sqlQuery();


    }

    function insert()
     {
        $query=new sqlQuery();

        $query1=new sqlQuery();
        $call=$query1->insert('active','10'); 


     }

  }

 Help me thanks in advance.....

Solution

  • Late static binding is about parent - children relationships. It's unclear what you want to get. You should have some subclass of sqlQuery to use late static binding. Something like

    class gallerySqlQuery extends sqlQuery
    {
        protected static $colum1=" status "; // better private or protected
        protected static $colum2=" order "; // you don't need it to be public
        protected static $colum3=" colum3  ";
        protected static $colum4=" colum4  ";
    }
    

    in the gallery class you should call the method of this class

    $query1=new gallerySqlQuery();
    $call=$query1->insert('active','10'); 
    

    Then, you don't need to declare all these public static $colum1 etc in the sqlQuery class.