Is the php class redundant?

Source: Internet
Author: User
{Code...} often encounters such a class definition and is not instantiated or called elsewhere (the test class or aaa function cannot be found in the global search ). Why should we define the areas that are not used? Defined. Where can I call it if it is useful?
Class test {function aaa () {echo "Unnecessary ";}}

After such a class definition is often encountered, it is not instantiated or called in other places (the test class or aaa function cannot be found in the global search ).
Why should we define the areas that are not used?
Defined. Where can I call it if it is useful?

Reply content:
Class test {function aaa () {echo "Unnecessary ";}}

After such a class definition is often encountered, it is not instantiated or called in other places (the test class or aaa function cannot be found in the global search ).
Why should we define the areas that are not used?
Defined. Where can I call it if it is useful?

Do class calls need to be new, and call methods must explicitly write the method name?

It may be a dynamic call, or a callback, which is an invisible call.

Test classes are more likely to be test classes that are not used in projects for testing by developers.

Php supports reflection, that is, it is better to allow dynamic class names and function names during runtime to load and call related class methods ~
As a dynamic language, the class name can be taken from user input and so on, and then loaded dynamically. Why write in the code?
For example, if you get a plug-in system namedxxxPlug-ins are placed in/plugin/xxx/xxx.phpAnd the plug-in class name isxxxHas a method calledinitSo the code is written like:

$ A = get the list of folder names under the plugin directory (); foreach ($ a as $ I) {$ file = "/plugin/$ I. php "; if (file_exists ($ file) include ($ file); else {...} if (class_exists ($ I) $ t = new $ I (); else {...} if (method_exists ($ t, 'init ') $ t-> init (); else {...}}

In this way, all plug-ins are loaded and initialized, but the source code is completely unavailable.xxxIsn't that true for this string.

@ MrGeneral: the answer is awesome. php has reflection.
In addition, one possibility is the legacy code, which will be used in the previous code, but some old code will not be used after reconstruction (used to modify the code, no one deleted them, so they kept them all the time.

It is reasonable to exist.

I think call_user_func is probably used in your framework.

The global search does not exist. It does not mean this is not used.
If you are not sure, you can try to comment it out first. However, based on past experience, it is recommended that you do not do useless things. If this class or function is only useless, then it does not occupy any resources, it may be useless to comment out at most.
Never delete code, never delete code, never...

You can write in _ construct ()Echo 'call? '; Exit;

Then let's take a look at how the program runs. (Of course, it may not be called during startup, but it is called in a very complicated corner in a specific environment situation)

The same is true for xdebug breakpoints.

It may be a final class.

Test code

$ Class = $ _ GET ['name']; // $ class = 'test'; $ method = $ _ GET ['method']; // $ method = 'aaa'; $ myclass = new $ class (); $ myclass-> $ method (); /*** defines the tst class */CLASS test {function aaa () {echo _ class __. "-> ". _ method316.php_eol ;}}

Access through a browser:

http://hostname/seg.php?name=test&method=aaa

The name here is not standard, just to illustrate the problem.
You cannot see this implementation method in the instantiation process.

If it is an injection framework, it is normal that the class does not find the place to be called.

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.