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

  • SEARCH
  • Home
  • 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 7434397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:49:56+00:00 2026-05-29T09:49:56+00:00

This is not really a problem but more of a question. My question is

  • 0

This is not really a problem but more of a question.
My question is how it’s ‘supposed to be’ programmed.
It might not be too clear to explain, though.

So my question is; do I have to make multiple methods to retrieve data from a database?
For example, I have a table called FRUITS.
It contains the ID, name and date the fruit was added.
Now I want to get the name of the fruit based on a given ID, and later on in the script I want to get the date of the fruit as well.

Should I make one method such as get_fruit($id) which returns both the name and date, or two separate methods get_name($id) and get_date($id)?

Thanks in advance.

  • 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. Editorial Team
    Editorial Team
    2026-05-29T09:49:57+00:00Added an answer on May 29, 2026 at 9:49 am

    You should use one object which would contain all the required data. For example:

    class Fruit {
    
        protected ... variables;
    
        public function getId() {...}
        public function getDate() {...}
        ...
    
    }
    

    Also implementing __set and __get would be nice example of using full php potential.

    You also may implement save() method (or extend database row class, such as Zend_Db_Table_Row.

    So whole code would look like:

    $fruit = $model->getFruid( 7); // $id = 7 :)
    echo $fruit->id; // would call internally $fruit->__get( 'id')
    echo $fruit->date;
    
    // And modification:
    $fruit->data = '2011-08-07';
    $fruit->save();
    

    EDIT: using separate methods to load certain data is useful (only?) when you need to load large amount of data (such as long texts) which is required only on one place in your code and would affect performance.

    EDIT 2: (answer to comment):
    __get and __set are called when you try to access undefined property of an object, for example:

    class Foo {
         public $bar;
         public function __get( $name){
             echo $name "\n";
             return 'This value was loaded';   
         }
    }
    
    // Try to use
    Foo $foo;
    echo $foo->bar . "\n";
    echo $foo->foo . "\n";
    

    There are two “large” approaches to this that I know about:

    // First use __get and __set to access internal array containing data
    class DbObject {
    protected $data = array();
    
    public function __get( $propertyName){
        // Cannot use isset because of null values
        if( !array_key_exits( $propertyName,$this->data)){
             throw new Exception( 'Undefined key....');
        }
    
        return $this->data[ $propertyName];
    }
    
    // Don't forget to implement __set, __isset
    }
    
    // Second try to call getters and setter, such as:
    class DbObject {
    public function getId() {
       return $this->id;
    }
    
    public function __get( $propertyName){
        $methodName = 'get' . ucfirst( $propertyName);
        if( !method_exits( array( $this, $methodName)){
            throw new Exception( 'Undefined key....');
        }
        return $this->$methodName();
    }
    }
    

    To sum up… First approach is easy to implement, is fully automatized… You don’t need large amount of code and sources would be pretty much the same for every class. The second approach required more coding, but gives you a better control. For example:

    public function setDate( $date){
        $this->date = date( 'Y-m-d h:i:s', strtotime( $date));
    }
    

    But on the other hand, you can do this with first approach:

    class Fruit extends DbObject {
    public function __set( $key, $val){
      switch( $key){
         case 'date':
           return $this->setDate( $val);
    
         default:
           return parent::__set( $key, $val);
      }
    }
    }
    

    Or you can use total combination and check for getter/setter first and than try to access property directly…

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

Sidebar

Related Questions

this is not really a problem I have, but more a question about good
This is not really a functional problem I'm having but more a strategic question.
This is not really a question about addressing a specific problem but more a
This is not really a question because I just solved the problem, but I
This is not really a programming question, more of an algorithmic one. The problem:
This is not really a programming question but please bear with me as I
This is not a really technical question but is required for a system I
This is not a really Programming Question, but please bear with me as it's
Not really too sure how to word this question, therefore if you don't particularly
This is not exactly a straight-out question because I have just solved it, but

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.