This Article introduces you to object-oriented programming in PHP. It explorer’s the basics of object-oriented programming, and you can learn how to write simple object-oriented PHP scripts and learn how to build robust object-oriented PHP applications quickly and easily. In this Article, we’re going to kick things off nice and gently by looking at some really basic concepts of object-oriented programming.
What object-oriented programming is? Object-oriented programming (OOP) is based on the concept of “objects”, which are data structures that contain data, in the form of fields, often known as attributes and code, in the form of procedures, often known as methods. A distinguishing feature of objects is that an object’s procedures can access and often modify the data fields of the object with which they are associated.
Why use object-oriented programming?
1.Easy to map onto real-world situations: Objects can easily map onto real-world “objects” such as people, products, shopping carts, blog posts, and so on. By this, life goes much easier when you set out to design your object-oriented applications, since the purpose of each object, as well as the relationship between objects, is already fairly clear.
2.Easy to write modular code: By separating your code into distinct modules, you make your code more manageable, and easier to debug and extend.
3.Easy to write reusable code: Writing reusable code can saves more time when writing an app, and over time you can build up a library of reusable code modules that you can use in many different apps. OOP makes easy to write reusable code, since functions and data structures are nicely encapsulated in a single, reusable object. It’s possible to take an existing object and by adding new functionality it can be extend for a specific application, which makes object easy to use again.
Some fundamental concepts, including classes, objects, methods and properties
4 fundamental OOP concepts: classes, objects, properties, and methods.
Classes: A class is a blueprint or template or set of instructions to build a specific type of object. When you will build your object-oriented application, you will typically create one or more classes representing various types of entities in your app. There are 3 types of property that you can add to a class: Public properties can be accessed — that is, read and/or changed — by any code in your script, whether that code is inside or outside the class. Private properties can only be accessed by methods within the class. It’s best to make your properties private if possible, since it helps to keep your objects separated from the rest of your code.Protected properties can be accessed by methods within the class, and also by code in classes that inherit from the class.
Objects: An object is a component of a program that knows how to perform certain actions and how to interact with other elements of the program. Objects are the basic units of object-oriented programming. A simple example of an object would be a person. Logically, you would expect a person to have a name. This would be considered a property of the person. You could also expect a person to be able to do something, such as walking or driving. This would be considered a method of the person. An object is often said to be an instance of a class, and the process of creating an object from a class is called instantiation.
Properties: The data values that hold by an object are stored in special variables known as properties. An object’s properties are closely tied to the object. All objects created from a given class which have the same properties, one object’s properties can have different values to another object’s properties.
Methods: The functions which are defined within a class and used in an object are known as methods. They’re just like regular functions. You can pass values to them, they can contain local variables, and they can return values. To call an object’s method, you have to use operator.
Here’s how the script works:
By call $member->isLoggedIn () to determine if the member is currently logged in.
IsLoggedIn () simply returns the value of the $logged in property.
Since $logged in defaults to false, isLoggedIn () returns false to the calling code, resulting in the message “Log Out”.