How to call the methods without creating the object?

There is the following code
class test{
 public $table;

 public function table(string $table)
{
 $this->table = $table;
 return $this;
}
 public function delete()
{
 echo $this->table;
}
}
$db = new test;
$db->table('users')->delete(); // users

How to do that without creating an object, what would the call was so
test::table('users')->delete();
June 14th 19 at 18:29
3 answers
June 14th 19 at 18:31
Solution
I think you cannot call methods from a non-existent object though, because the properties that they use are not initialized, but without creating an object you can call static methods
There is a solution, view can come in handy. - Candido84 commented on June 14th 19 at 18:34
June 14th 19 at 18:33
Solution
For example:

class Test
{
 private $tableName;

 public function __construct($tableName)
{
 $this->tableName = $tableName;
}

 public static function table($tableName)
{
 return new self($tableName);
}

 public function delete()
{
 // do something
}
}
thanks, works, wanted to say error but corrected) - Candido84 commented on June 14th 19 at 18:36
If you're talking about the person's answer above that this is not a solution, it creates a new object in a static function - Candido84 commented on June 14th 19 at 18:39
I you did not understand correctly, Yes, you are right, but my task, though with the creation of the object inside the method, still partially solves. - Alene56 commented on June 14th 19 at 18:42
That's a decision that I wanted to see the vehicle. He incorrectly expressed my thought, most likely due to the fact that he does not understand the mechanism of such structures:
test::table('users')->delete(); - Candido84 commented on June 14th 19 at 18:45
the method table must return the object and nothing else, which hereinafter will be called the delete method. Helped, thanks, but clever it's not necessary. - Manuela.Ho commented on June 14th 19 at 18:48
Where did I get smart? I said it like it is. - Candido84 commented on June 14th 19 at 18:51
I don't understand the mechanism designs? Are you so sure? - Manuela.Ho commented on June 14th 19 at 18:54
If you understand how it works, you would not ask the question. - Candido84 commented on June 14th 19 at 18:57
it you asked a question, if I understand everything, why ask... - Manuela.Ho commented on June 14th 19 at 19:00
I wanted, and asked. - Eloise.Hamill commented on June 14th 19 at 19:03
in case that someone will tell me something I don't know. - Candido84 commented on June 14th 19 at 19:06
can you suggest to read about psr - Candido84 commented on June 14th 19 at 19:09
thank you) - Eloise.Hamill commented on June 14th 19 at 19:12
I create an object add to a static variable, and then created only on the first call. - Candido84 commented on June 14th 19 at 19:15
Yes , what did you expect to hear? - Candido84 commented on June 14th 19 at 19:18
June 14th 19 at 18:35
Solution
(new test)->table('users')->delete();

Find more questions by tags PHP