Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 126127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:16:06+00:00 2026-05-11T05:16:06+00:00

How can I do this in PHP $myDBClass->users()->limit(5);//output you limited users to 5 $myDBClass->comments()->limit(3);//output

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:16:06+00:00Added an answer on May 11, 2026 at 5:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can accomplish this in PHP but I really need to have
To grab the inode of a file in PHP, you can use this: $fs
I know I can do this: IDateTimeFactory dtf = MockRepository.GenerateStub<IDateTimeFactory>(); dtf.Now = new DateTime();
I'm not sure if this is a trivial questions but in a PHP class:
How can I simplify this PHP condition? <?php function authUser( $user, $pass ) {
Can this be done by setting a property? I'd prefer that approach then to
Can this be done w/ linqtosql? SELECT City, SUM(DATEDIFF(minute,StartDate,Completed)) AS Downtime FROM Incidents GROUP
How can this line in Java be translated to Ruby: String className = java.util.Vector;
This isn't working. Can this be done in find? Or do I need to
MySQL has this incredibly useful yet proprietary REPLACE INTO SQL Command. Can this easily

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.