Y
Y
yellow_pus2021-11-30 10:24:33
PHP
yellow_pus, 2021-11-30 10:24:33

How to continue a chain of requests in php?

I'm learning MySQL at the moment and noticed that we can access "internal queries" like in this example:

$mysqli->query("CREATE TABLE test(id INT, label TEXT)");

As far as I know, such a query can be built using OOP. I tried, but I didn’t succeed and I’m confused, tell me where is the mistake in my “design”
class A
{
    public function get()
    {
        $sql = "SELECT * FROM users WHERE id = 123";
        return $sql;
    }
}
class B extends A
{
    public function add($sql)
    {
        return $sql .= " AND name = 'Alex'";
    }
}
$a = new A();
$b = new B();

echo $b->get()->add();

Here, on the last line, I'm trying to add an additional request to the previous one. condition

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-11-30
@yellow_pus

<?php
class A
{
  protected $sql = null;
  
  public function setSQL()
    {
        $this->sql = "SELECT * FROM users WHERE id = 123";
        return $this;
    }
  
  public function getSQL()
    {
        return $this->sql;
    }
}

class B extends A
{
    public function addSQL()
    {
        $this->sql .= " AND name = 'Alex'";
    
    return $this;
    }
}
$a = new A();
$b = new B();

echo $b->setSQL()->addSQL()->getSQL();

PHP class test

I
Ipatiev, 2021-11-30
@Fockker

If we discard all the nonsense that is not related to the question, such as some kind of "internal requests" (?!), then the answer comes down to a chain of calls .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question