Descarga la aplicación para disfrutar aún más
Vista previa del material en texto
Object-Oriented Programming in PHP for Beginners Ochoa Preciado Enrique de Jesús Universidad de Colima, Ingeniería en Software, 4°D. March 09, 2022. eochoa11@ucol.mx Abstract—Oriented-Object Programming in different languages was a very useful tool, because makes easier the coding for different web pages and software. The programming language PHP It’s the most used language to make different web pages and web applications; we can use O.O.P to optimize and makes clean code easy to read. There are different concepts to learn before starting to code in PHP OOP, likes: classes, objects, methods, properties, and constructors. I. INTRODUCTION When we talk about programming, exists different ways to code: Procedural Programming and Object-Oriented Programming. Procedural Programming was chunks of code trying to achieve a single purpose into a function, then we name the function with related name and call back after. In OOP (Object-Oriented Programming) we create Classes, then create Objects (instance of the Class) from the class we created. The OOP helps to improve our code and make easier insert new code or functionalities inside the existing source code, because we can write new code instancing the Class created; and we make new Classes and Objects for different purposes too. II. OBJECT-ORIENTED PROGRAMMING Object Data Programming uses different concepts that we need understand to use later. We can find these concepts: A. Class Is like a blueprint or a template that we create to achieve a purpose. Here, we can define methods or properties that we need to achieve the functionality we want. B. Object An Object is essentially an instance of a Class. It’s like declaring a variable with a string value, integer, or Boolean value. When we declare an Object, we already have all the properties or methods of the class. C. Methods Are basically functions. Like how you write a function in procedural programming. There are functions to achieve something be it connecting to a database, fetching data from a database, or maybe writing a function to consume a REST API. D. Properties Are like attributes. When we write a Class, we want the class to have a value of something (width, height, color…) E. Constructor The constructor helps to run a function whenever we create an Object of the Class. F. Access Modifiers This access modifiers allows to access to properties or methods in class. These modifiers can be public, private, or protected. III. OOP VS PROCEDURAL The OOP was a different way to code different web or desktop software. It is a subjective opinion for the use of this different ways to code (and depends on the circumstances). The author does not state that one way is better than the other, however, he recommends that new coders learn to use OOP because it is vitally important to know both ways of coding. In the case of this article, it shows us how to use object-oriented programming on PHP, a language widely used in the development of web applications around the world. With this we can implement the written code to practice a little and understand the basic concepts to differentiate between one method and another. Object-oriented programming can be found in many programming languages, offering many alternatives to writing functionalities or code implementations to obtain a certain function within our application. Some of these languages that can use OOP are C++, Java, Python, PHP, C#, Kotlin, TypeScript, Per, Ruby, Objective C, and a few more. This is an example of procedural programming: Fig 1. Procedural Programming Example We have a lineal structure, defining functions declaring variables and calling back. Then, we have the traditional method for programming different functions or apps to make different activities or obtain different results. In the Fig 2, we can see the difference of the OOP with different structure. IV. CONSTRUCTOR IN A CLASS Fig 2. OOP Structure. What is a constructor? Is a special function that we declare in Class with the name __construct ()? Is a function that will always get call automatically every time we create an instance of a Class? Check the Fig. 3 and let’s see the structure. Fig. 3 Constructor example. We have three arguments in the Class. We only have to declare like so $rasyue = new Rasyue(‘args1’, ‘args2’). The Constructor that we define in Rasyue will in turn set three properties of the class to the value of the three inputs that we pass in during the instantiation of the class. V. CONCLUSIONS The OOP it is a useful method for the coding of functions that we need in the same object or establish different functions for each object in the code. In addition, it allows us to save code and the easy insertion of new lines of code that allow other functionalities to be carried out within the code that already exists. Later, we can find that by having objects that have the same attributes, it is only necessary to call them from another page to reuse the elements that we need. REFERENCES [1] Rasyad (2020) Learn Object-Oriented Programming in PHP. [Online]. Available: https://medium.com/swlh/object-oriented-programming-in- php-for-beginners-5887ee58eefc https://medium.com/swlh/object-oriented-programming-in-php-for-beginners-5887ee58eefc https://medium.com/swlh/object-oriented-programming-in-php-for-beginners-5887ee58eefc
Compartir