What are the advantages of this PHP notation?

Source: Internet
Author: User
Keywords Php
Often see some third-party libraries that have this kind of notation
Give me a chestnut.

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

So when I call in the code, if I want to output the json format of the data, you must first tune setType this method

$this->setType('json');.........$this->display(); //这个display里判断$outType属性是`html`还是`json`

What I'm puzzled about is, why not display() pass the parameter directly in the method, such as the following

$this->display('json');

This also saves a method call. I want to know where the benefits of the first form of writing are mainly expressed .

Thank you, everyone.

Reply content:

Often see some third-party libraries that have this kind of notation
Give me a chestnut.

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

So when I call in the code, if I want to output the json format of the data, you must first tune setType this method

$this->setType('json');.........$this->display(); //这个display里判断$outType属性是`html`还是`json`

What I'm puzzled about is, why not display() pass the parameter directly in the method, such as the following

$this->display('json');

This also saves a method call. I want to know where the benefits of the first form of writing are mainly expressed .

Thank you, everyone.

The first way: you can interpret it as:

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

But now the whole becomes class! Because this class in the outside is inherited calls, it may be different depending on the caller, such as: need to output HTML or JSON format, but I can not directly modify protected $outType the value, it must have a way to indirectly modify the properties inside the class, so there is the following setType($type) method, The effect of modifying internal property values is achieved by external calls!

The object-oriented notation is particularly noticeable in Java.

The benefits are, of course, encapsulated and secure.

Especially in third-party class libraries It is obvious that encapsulation !

As a library function, you just have to tell someone what you have, what features you can provide, the variables used internally are not exposed as much as possible, and the user only cares about the function.

What if I have to pass in a josnd (mis-operation)?
All kinds of parameter testing did you get the display to explode? Display clearly as long as a type is good, tube so much what to do?
Layering, encapsulation!

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

The difference between object-oriented programming and functional programming;

Search for the answer:

Programming comes to the operation of two aspects, data and data,

The advantage of OOP, which is to have operation around data, is that it's handy when you want to add a new data type! The original code is not changed. But what do you do to add a method to the already-written data type? For example, I want you to add a python-like multiplication to the string that comes with Java.

Functional programming takes another thought, data more around operation, so adding new methods is easy. This is the famous expression problem. Who is inferior, want to see the application scenario, write GUI with OOP is not carefree, write interpreter I prefer function type.

It seems to me that these are idiom, not religious, specific problems specific analysis. But learning functional programming absolutely helpful, because the domestic computer education too stressed Turing model (that is, C language one vein), and the lambda calculus involved too little, resulting in a lot of prejudice and misunderstanding, I have not only once seen on the knowledge, learn C language , the argument that other high-level languages are easy to understand is obviously tainted by this kind of education.

Author: Zhang Mowgli Link: https://www.zhihu.com/question/19732025/...
Source: Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

It is only a matter of words to be trampled by others.

Because display should not change state.

In fact, if this settype is just a simple thing to do, that is over-encapsulation, if the Settype also do a parameter check, error exception handling, and so on, as you say, a way to do one thing, combined with specific things to see, will find the difference, Do not do this is not not, in short, the final want is the code logic clear, good maintenance, modification, the person behind the people can understand, not laborious.

Put aside the need to talk about code, are bullying

The way you say it, pass it, it's definitely possible.
However, if you use this parameter in the global, you do not assign the value to the attribute, but still pass, then you think, is the argument convenient, or directly in the method call property convenient, object-oriented programming, this parameter is the object's properties.
When this parameter is used only in the current method, you can also not assign a value to the attribute.

Both support is not better, settype as the default value, display parameters higher priority, code changes are also very small

Because the output format is not every call needs to change, such as the execution of the process, to multiple output, that is more common is the format of multiple output is the same, so with Settype () in the way you do not need each display to pass the format as a parameter;
In fact, there is room for improvement in this way: JSON and HTML output can be the default type, and other output formats pass an object that implements the display () interface, so that the output can be infinitely extensible.
Essentially, this is done to maximize the reuse of objects.
What is the reuse of objects? When an object is instantiated, it can be used more than once without changing any of its properties.

Seters/geters
The downside is that you write more code (slow development)
The advantage is enhanced encapsulation (improved maintenance capability)

One or two such packages will not matter, right, look at the higher maintenance of the face also endure.
If there is more, then write another load() way to load the group.

You are opposite to the object not cold!!

Personal feeling is mainly the problem of reuse, if only use this time outtype, then directly in the display as a parameter is indeed more convenient

But if more than one method is used to Outtype, I feel it would be more convenient to set it as an object's property.

The first thing you have to understand is that the author of this class thinks that at some point you just have to deal with one format, not HTML or anything else, like JSON, so you instantiate an object and format it as JSON, and then you're ready to use it. However, if many people find it necessary to constantly switch the output format in the actual use process, the class is not very well written, it is bad, it is better to write some tool classes, or directly in the display output format.
I personally recommend that you do not do any switching in HTML and JSON, directly define two pairs of objects, a processing of HTML one processing JSON, it is better, but I do not know whether the two objects are changeable, if it is changeable, it is not possible to write a parameterized display method

  • 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.