What are the advantages of PHP?

Source: Internet
Author: User
It is often seen that some third-party libraries have such a syntax as {code ...} so when I call the code, if you want to output data in json format, you must first call the setType method {code ...} what I don't understand is why I don't directly pass a parameter in the display () method... it is often seen that some third-party libraries use this method.
Example

protected $outType = 'html';public function setType($type){    $this->outType = $type;}

If you want to outputjsonFormat data must be adjusted oncesetTypeThis method

$ This-> setType ('json ');......... $ this-> display (); // In this display, determine whether the $ outType attribute is 'html 'or 'json'

Why not?display()Directly pass a parameter in the method, as shown below

$this->display('json');

This saves a method call.I want to know the main advantages of the first writing method..

Thank you.

Reply content:

It is often seen that some third-party libraries use this method.
Example

protected $outType = 'html';public function setType($type){    $this->outType = $type;}

If you want to outputjsonFormat data must be adjusted oncesetTypeThis method

$ This-> setType ('json ');......... $ this-> display (); // In this display, determine whether the $ outType attribute is 'html 'or 'json'

Why not?display()Directly pass a parameter in the method, as shown below

$this->display('json');

This saves a method call.I want to know the main advantages of the first writing method..

Thank you.

First, you can understand it:

function demo($type = 'html'){    return $type;}

But now it's a class! When this class is inherited from the outside, it may be different from the caller, for example, html or json format needs to be output, but I cannot directly modify it.protected $outType, There must be a way to indirectly modify the attributes in the class, so the followingsetType($type)Method. You can use an external call to modify the internal attribute value!

Object-oriented writing is particularly evident in java.

Of course, the benefits include encapsulation and security.

Especially in third-party class libraries,Encapsulation!

As a library function, you only need to tell others what functions you have and what functions you can provide. Internal variables used should not be exposed as much as possible. Users only care about functions.

If I want to inputjosnd(Misoperations), what should I do?
Does various parameter detection make display burst? Display only needs one type. What should I do?
Layered and encapsulated!

public function display($type = 'json'){    $this->type = $type;    .....}

Differences between object-oriented programming and functional programming;

I found this answer on zhihu:

Programming involves two aspects: data and operation on these data,

Oop is to make operation focus on data. The advantage is that it is convenient to add a new data type! You do not need to change the original code. But what should you do if you want to add a data type that has already been written? For example, I want you to add python multiplication to the string that comes with java.

Functional programming adopts another idea. data is more centered on operation, so it is easy to add new methods. This is the famous expression problem. Who is superior or inferior? It depends on the Application Scenario. I prefer functional programming when writing GUI with oop.

In my opinion, these are all idiom, not religion. Specific issues are analyzed. However, it is absolutely beneficial to learn functional programming, because the domestic computer education emphasizes the Turing model too much (that is, the one pulse of C language), and lambda calculus involves too little, resulting in many prejudices and misunderstandings, I have seen more than once in zhihu that the argument that I learned C language well and other advanced languages are easily understood is obviously suffering from this kind of education.

Author: Zhang Wuji link: https://www.zhihu.com/question/19732025...
Source: zhihu copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

This is what people say.

Because the display status should not be changed.

In fact, if this setType is just a simple task, it is overly encapsulated. If this setType also performs parameter checks, error exception handling, and so on, as you said, if you try to do only one thing in a single method, you will find the difference. If you do not do this, it is not impossible. In short, the final thing you want is that the Code logic is clear and well maintained, modification, the people behind it can understand it, and it is not difficult.

Despite the need to talk about code, they are all rogue

The method you mentioned, transfer, is certainly acceptable
However, if this parameter is used globally and you do not assign a value to the attribute, but pass the parameter, you can think about whether it is convenient to pass the parameter or directly call the attribute in the method, object-Oriented programming. This parameter is the object property.
If this parameter is only used in the current method, you can leave it unspecified.

The two types of support are not better. setType is used as the default value. The display parameter has a higher priority and the code is slightly changed.

Because the output format does not need to be changed every call. For example, if multiple outputs are required during an execution, the output format is the same, therefore, the settype () method does not need to pass the format as the parameter once each display;
In fact, there is room for improvement in this method: json and html output can be used as the default type, and other output formats are passed to an object that implements the display () interface, in this way, unlimited scalability can be achieved during output.
Essentially, this approach aims to maximize the reuse of objects.
What is the reuse of objects? After an object is instantiated, it can be directly used multiple times without changing any of its attributes.

Seters/geters
The disadvantage is that you write more code (lower development speed)
The advantage is enhanced encapsulation (enhanced maintenance capability)

It doesn't matter if you have one or two such packages, right? It's enough to endure it when it comes to higher maintainability.
If there is more, write anotherload()Methods for collective loading.

You are not familiar with object targeting !!

I personally think it is mainly about reuse. If you only use this outtype, it is more convenient to directly use it as a parameter in display.

However, if more than one display method uses outtype, I feel it is more convenient to set it as an object property.

First, you must understand that the author of this class thinks that at a certain time point you only need to process one format, not html or others, such as json, So you instantiate an object, and set the format to json, so it will be used directly in the future. However, if many people find that they need to switch the output format continuously during actual use, this class is not very well written. It is very bad. It is better to write some tool classes or directly input the output format in the display.
I personally recommend that you do not perform any switching in html and json. You can define two pairs of objects, one for processing html and the other for processing json, which is better, however, I don't know whether these two objects are changeable. If they are changeable, it is better to write a display method with parameters.

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.