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 7520685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:06:42+00:00 2026-05-30T02:06:42+00:00

Im preparing API methods for one of the system. Api will be available for

  • 0

Im preparing API methods for one of the system. Api will be available for few languages but first version will be released for the php. Im wondering what naming convention should use for accessor methods. I would like to have api interface very similar or even equal for each language. I was reading some libraries for php and noticed two naming convetion :

1) public accessor as set*() and get*() methods

class MyClass { 
  private $data; 
  public function getData() { 
    ...
  }

  public function setData($value) {
    ...
  }
} 

2) drop out set/get prefix

class MyClass { 
  private $data; 
  public function data($value = null) { 
    if (!empty($value) && is_array($value)) {
        $data = $value;
    }
    return $data;
  }
}

Im not php programmer, I have experience in java and c/c++ so I would like to ask about suggestions which way to go for php ? Especially what is more readable, clear and understandable for php programmers.

I would like to read comments and sugestions.

all the best.

ps. if the topic is duplicated im sorry, would like to ask to point me to original topic.

  • 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-30T02:06:42+00:00Added an answer on May 30, 2026 at 2:06 am

    Try this as a starting point. You will need to do more, but this should give you an idea, how to use __get and __set if this is an option.

    class MyClass {
    
      protected $data = array();
      protected $acceptedKeys = array('name', 'job', 'sport');
    
      public function __get($key) {
        if(isset($key) &&  array_key_exists($key, $this->data)) {
          return $this->data[$key];
        } else {
          throw new Exception("can not find {$key}");
        }
      }
    
      public function __set($key,$val) {
        if(in_array($key, $this->acceptedKeys)) {
          $this->data[$key] = $val;
        } else {
          throw new Exception('we will need a key value pair');
        }
      }
    }
    
    try {
      $myClass = new MyClass();
      $myClass->job = 'sports';
      $myClass->name = 'test';
      print $myClass->job . "\n";
      print $myClass->name;
    } catch(Exception $e) {
      print "error : " . $e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am preparing to use continuous integration for the first time. I will be
I am working on one project in which I am preparing API to interact
I'm preparing a string that will be eval 'ed. The string will contain a
I'm preparing for an interview in a few weeks and I thougth I would
While preparing my phone app for release, I thought I'd change the version numbers
Im preparing website page structure but stuck here Html <div id=main> <div id=left></div> <div
I am preparing for SCJP and I came to know Methods with variable argument
I'm preparing my paypal system and have a separate page that forwards the user
I'm planning on preparing some methods that returned me an already filtered collection of
When I was preparing for TOGAF exam, I came across Open Standard available for

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.