2

I would like to call a function within a class method, I created this script but not work as I can do?

Fatal error: Call to a member function function2() on null in /.../.../index.php on line 12

    public static function table($name_t, callable  $callback){
    self::databaseConnection();
    try {

        $matches = array(
            .....
        );


        function engine($var){
            Gaia::$engine__ = $var[0];
        }

        $eng = new Table_call;
        echo $eng;


        $callback($matches);

        if(isset(self::$s)){
            //self::$instance->exec("CREATE TABLE IF NOT EXISTS ".$name_t."( ".trim(self::$s,',')." ) ENGINE=MyISAM DEFAULT CHARSET=utf8;");

            echo "Dump Success!<br> ".self::$engine__ ;
        }

        //return $bgj;
    } catch (Exception $e) {
        self::$instance = null; 
        echo ("Oh noes! There's an error in the query: ". $e);
    }

}

Class File 2

class Table_call extends Gaia{

     public function __call($name,$arg){
      call_user_func($name,$arg);
     }
 }

Index File

Gaia::table('test', function($table){
   $table['autoIncrement']('id');
})->engine('MyISAM');

how can i add function this way ??

" ->function2('hello') "?
  • Static functions needs to be requested through `::`, while NOT static functions needsd to be requested with `$this->`. So in your case it would be `self::function();` if you make it a not static function it would be `$this->function();`. Will this help you out? Some extra explanation: https://stackoverflow.com/questions/1417438/what-are-static-and-dynamic-variables-methods-in-oop – Ronnie Oosting Nov 14 '17 at 18:22
  • Can you post the whole html code where you try to echo the php code? – Ronnie Oosting Nov 14 '17 at 18:32
  • ok sorry, updated. – Francesco Esposito Nov 14 '17 at 18:39
  • You're not returning anything in the method.. Generally, chaining requires a return. – FirstOne Nov 14 '17 at 18:51
  • 1
    Related? [Chaining Static Methods in PHP?](https://stackoverflow.com/q/125268/4577762) (maybe a dup) – FirstOne Nov 14 '17 at 18:51
  • I tried : $ufsd = new self; self::$instance->exec("CREATE TABLE IF NOT EXISTS ".$name_t."( ".trim(self::$s,',')." ) ENGINE=".$ufsd." DEFAULT CHARSET=utf8;"); but i got this error: Catchable fatal error: Object of class Gaia could not be converted to string in ... – Francesco Esposito Nov 14 '17 at 20:20
  • Not sure, but in my opinion you should not call Gaia statically. So it like this: Bob is selling his laptop to Charley. But to get the Laptop to Charley it has to be through drop shipping. So Bob drops his Laptop to a post order company, the post order company sends (from the received address from Bob) the Laptop to Charley. Charley received the laptop (data). Meanwhile Charley can not do anything from here, except use the Laptop. The post order company will `return` a message if the delivery has been sent, or not. – Ronnie Oosting Nov 15 '17 at 08:21
  • Which is an `true` or `false`, or if possible a note from Charley, like 'Thanks!'. – Ronnie Oosting Nov 15 '17 at 08:21
  • So what I mean: You need to send data to a function, this function receives the data and handles it, then the out come needs to be returned. Like: `public function giveMeData($data) { // $return = Do stuff; return $return; }`. So the function `giveMeData` receives information, and will `return` some results to the requested function. I hope this clears a bit on what you need. Try to use this correctly, Information about self:: and $this-> can be found here: https://stackoverflow.com/questions/151969/when-to-use-self-over-this – Ronnie Oosting Nov 15 '17 at 08:24
  • `return $eng` if the `->engine('MyISAM')` exists inside `Table_call` although I have no idea what `function2` is or what you mean by it. – ArtisticPhoenix Nov 15 '17 at 18:43

1 Answers1

0

I don't know if this is what you want but it's the smallest example I could do. It's not clear to me in your question what exactly you are trying to do as there is no function2 in any code you posted so I have to guess what that is. Anyway...

<?php
class Gaia{
    public static function table(){
        return new Table_call();
    }
}

class Table_call extends Gaia{

    public function function2(){
        echo __METHOD__;
    }

}

Gaia::table()->function2();

You can try it here.

http://sandbox.onlinephpfunctions.com/code/dd1c5db302448e8f075a081acfc33cb00c3f71b5

Looking at your code, your not returning any objects to call the second function on. Think about it this way

$var =  Gaia::table();
 //var is an instance of Table_call
$var->function2();

Also your error

Fatal error: Call to a member function function2() on null in /.../.../index.php on line 12

The default return type is NULL so...

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38