Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a PHP nested class or nested methods?

How can I do this in PHP

$myDBClass->users()->limit(5);//output you limited users to 5
$myDBClass->comments()->limit(3);//output you limited comments to 3

what I meant is nested methods or nested class (I don't know!) so when I call the limit method as a child of users it will know that I am calling it from "users" method -or class- and when I call limit method -or class!- from comments It also knows that.

what is the possible structure for a PHP class to do this thing?


the reason for this question because I am working on my own class for database so I can easily use something like this

     $DB->comments()->id(" > 3")->limit(10);

to generate the sql code "select * from comments where id > 3 limit 10" Thanks

like image 422
ahmed Avatar asked Feb 10 '09 18:02

ahmed


People also ask

Can classes be nested in PHP?

PHP 7 has introduced a new class feature called the Anonymous Class which will allow us to create objects without the need to name them. Anonymous classes can be nested, i.e. defined inside other classes.

How do you create a nested class object?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

What is nested class with example?

A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class.


2 Answers

Have the methods return objects with the methods described, and you get what you are after.

So, as long as $DB is an object that has a comments()-method, that part is valid. If that comments() returns an object that has an id()-method, that part is valid, too. Then, id() needs to return an object that has the limit()-method.

In your particular case, you might want to do something like this:

class DB {
  public function comments() {
    // do preparations that make the object select the "comments"-table...
    return $this;
  }

  public function id($string) {
    // handle this too...
    return $this;
  }

  public function limit($int) {
    // also this
    return $this;
  }

  public function execute() {
    $success = try_to_execute_accumulated_db_commands();
    return $success;
  }
}

$DB = new DB();
$DB->comments()->id(" > 3")->limit(10);

In my example, every method (also not depicted here) would return the object itself, so that commands can be chained together. When the construction of the database query is done, you actually evaluate the query by invoking execute() that (in my case) would return a boolean that would represent the success of the database execution.

User nickohm suggested that this is called a fluent interface. I must admit that this is a new term for me, but that tells probably more of my knowledge, than the term's usage. ("I just write code, you know...")

Note: $this is a 'magic' variable that points to the currently active object. As the name suggests, it just returns itself as the return value for the method.

like image 84
Henrik Paul Avatar answered Oct 18 '22 01:10

Henrik Paul


The standard convention for this is to return the instance of $this at the end of each of the method call. So when returned to the caller we are then just referencing another method call.

class Foo
{
  public function do_something()
  { 
    return $this; 
  }

 public function do_something_else() 
 {
   return $this; 
  }
}

$foo = new Foo();
$foo->do_something()->do_something_else();
like image 22
Antonio Haley Avatar answered Oct 18 '22 01:10

Antonio Haley