How to write a good interface method

Source: Internet
Author: User
Intend to write these interfaces for others to use, think of two methods, do not know the kind of better

The first method interface, this kind of expansion is troublesome, and the position is fixed
Public function Student ($name, $age, $sex)


The second method of interface, write array
Public function student ([' name ' = $name, ' age ' = ' $age, ' sex ' + $sex])


Excuse me, that's better.


Reply to discussion (solution)

As an interface, of course, to explicitly specify the parameters
Can't I just get a parameter and get the result?

Recommend the first kind.
The second is easy to use, but if teamwork is troublesome.

As an interface, of course, to explicitly specify the parameters
Can't I just get a parameter and get the result?



Also, the main method inside the argument location is fixed dead, not very friendly.

In fact, interface is the object of communication between the norms, no rules inadequate surrounding area
Your second plan is not without merit, but because of the constraint is not strong, you need to use caution

Recommend the first kind.
The second is easy to use, but if teamwork is troublesome.



However, the elements of the method need to be encrypted in order to prevent changes in the transmission process, $params is the number of groups [$name, $sex, $age]
Yii:: $app->siteapi->check ($functionName, $params =[], Encryption::encrypt ($params))

I think it's up to you to look at your needs, assuming you have registered accounts, mailboxes, names, and system-generated userid, then the method should be
function register ($user _id, $email, $name)

But if the user information is updated, and the user information includes a lot of such as phone, nickname, QQ, school, gender, hobbies and so on, and can support a single field update, then it is best to pass an array
function Update_info ($user _info)

The second better, easy compatibility with the old version, the extensibility is very strong, suitable for the continuous iterative system

It's hard to say, look at the situation.
In the first form, the specification
In the second case, replace the array with the object:

Interfacepublic function Student (student $student)//classpublic function student (student $student) {    $this- >student = $student;};/ /studentprotected $data = [    ' name ' = ' xxx ',    ' age ' = ' 0 ',    ' sex ' = 0,];//geter//seter

It is not very good to use arrays directly without restrictions. There is no limit, sometimes it is convenient, but things are too much, messy.

The advice of my friends upstairs is very good.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.