The variables defined are called properties. From now on, we will use the appropriate terminology. The
The best way to understand how a class works is to instantiate the class, call the method, and follow the code. When you create a post, the creator is run. You have seen how to populate these two attributes. Then call $post->the\u except(). Now, when writing code outside the class itself, use object variables instead of $this. This function simply reflects the return value of another function $this->get\u extract()$ As you know, because this keyword is used to call this function, you must look in the class. The
The get\u extract() function gets the content from the defined $content variable, so there is no need to pass the content. Then run our little excerpt and return the string. Before we learn how to use classes in the course summary plug-in, let’s give a brief overview. The current class is the rapper of the function. Enter the class name after class to define. Create functions (named methods) and variables (named attributes) within braces. If you write code within a class, you can use the $this keyword to reference properties and methods defined in the class. The
You can create objects using the $object=new class ($params) syntax. At this point, the constructor will run inside the class that can be used to populate the property, if necessary. To perform functions on objects available in a class, use object variables. If you want to learn more about objects, use tutsplus. Check out com or many other OOP PHP tutorials. Using object classes to encapsulate code in plug-ins helps prevent code conflicts. Although WordPress has its own function with the same name, classes in the WordPress plug-in can have the get_extract() function. The
OOP is an excellent encoder because it requires better conventions. The result is that the code is easier to read and organize. OOP can be easily extended using subclasses and other mechanisms, so operations are potentially more modular. OOP is almost always a better solution if written well. To gain advantage, you usually need a code base larger than a few lines, but a simple word press plug-in is a perfect exercise. It is recommended to use objects as a simple encapsulation mechanism at startup. The following is a simple example of adding a \
I like to put the code into the 9c3b6a2656d15c2832bf344358a2ae27 object of the loading summary, and then automatically hook the call in a more organized way. The pattern is that you must hook all tasks and filters into the creator function, and then create a function under it. If you follow this pattern, remember to use the array as the second argument to the add\u filter() and add\u action() functions. The first attribute must be $this, and the second attribute must be the name of the function. Once you are familiar with this technology, you can start playing with things. You can create classes for processing plug-in front-end output and classes for back-end changes. The
View this plug-in and modify the WordPress site to display all posts sorted by number of posts. Load summary 9c3b6a2656d15c2832bf344358ae27 this example may seem a little too complex (therefore), but the point I want to make is that it makes sense to separate the code into well-defined areas. It will be easier to extend the operation and modify it later. If you want to know more detailed examples of complete examples, WordPress plugin boiler
View the plate project. Try to put the plug-in in the appropriate code. I have been using it in many projects, especially for plug-ins that require a higher level of complexity. I can sincerely recommend it. Conclusion WordPress needs better coding plug-ins. One solution is to use object-oriented code. It makes your code easier to read, better organized, and generally of higher quality. You can easily extend tasks, and if you are a skilled OOP encoder, you can almost document it. Have you ever used OOP in plug-ins? Has this tutorial inspired you to start? Please tell us the following comments. Labels: passwords