1

I have below scenario, want to use specific method as static so can i use $this for other methods or not.

class foobar {
    public $foo;

    public function __construct() {
        global $foo;

        $this->foo = $foo;
    }

    public static function foobarfunc() {
        return self::foo();
    }
    public function foo() {
        return $this->foo = 'hi';
    }
}
echo foobar::foobarfunc();

ERROR:

Fatal error: Using $this when not in object context in

This is my scenario

class DB{
    public function selectQuery(){
        $data = $this->finddata();
        return "SELECT $data FROM bhumi";
    }
    public function finddata(){
        $data = ('*');
        return $data;
    }
}
class TP extends DB{
    public static $create;
    
    public function __construct(){
       $this->parentObj = new DB();
    }
    public static function printQ(){
        $d = parent::selectQuery();
        return $d;
    }
}
echo TP::printQ();

How to do that? this code giving me error.

Caconde
  • 4,177
  • 7
  • 35
  • 32
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • What is the error? – Aefits Feb 28 '18 at 13:00
  • "_this code giving me error._" What error? – brombeer Feb 28 '18 at 13:00
  • return self::foo(); <-- foo() isn't static. You need to create an instance first – Johan Feb 28 '18 at 13:00
  • what if I don't want to create an instance. – Bhumi Shah Feb 28 '18 at 13:04
  • Why using static to return result of classical instance ? make no sens.. – Fky Feb 28 '18 at 13:06
  • When you dont wat to create an instance, foo() should be static too. See my answer – Johan Feb 28 '18 at 13:08
  • I simply don't get what are you trying to achieve. [There is a difference between static and instance methods](https://stackoverflow.com/questions/30402978/php-static-vs-instance-method) – Aniket Sahrawat Feb 28 '18 at 13:10
  • @AniketSahrawat: I want to use non-static and static method together without creating an external instance. – Bhumi Shah Feb 28 '18 at 13:16
  • You should create an instance inside your class. See AliShojaei's answer. But still it does not make sense to me unless you say that you want to create a static factory method as `foobarfunc`. See [this](https://stackoverflow.com/a/8959150/6099347) answer. [You can also make it singleton](https://stackoverflow.com/questions/3319434/singleton-pattern). – Aniket Sahrawat Feb 28 '18 at 13:44

3 Answers3

1

try this way

public static function foobarfunc() {
    $foobar = new self();
    return $foobar->foo();
}
ishegg
  • 9,685
  • 3
  • 16
  • 31
ali shojaei
  • 84
  • 1
  • 5
1

When you dont want to create an instance, ignoring the reasons why:

<?php 
 class foobar {
public static $foo;

public function __construct() {
    global $foo;

    $this->foo = $foo;
}

public static function foobarfunc() {
    return self::foo();
}
public static function foo() {
    return self::$foo = 'Hi!';;
}
}
echo foobar::foobarfunc();
?>
Johan
  • 931
  • 9
  • 23
0
public static function foobarfunc() {
    return self::foo();
}

You just want to load another static method called "foo". This method exist and is not static, so the error is valid.

You can fix it with

public static function foobarfunc() {
    $instance = new self();
    return $instance->foo();
}

Albeit, the usefullness of this use be left open.

Brain Foo Long
  • 2,057
  • 23
  • 28