Creating Classes and Objects in PHP
Classes and objects are actually really easy to create in PHP. To work with the OOP we need to create the class first and then after we will create the object of that class. To create a class, you use PHP’s class keyword.
Now your next task is to create the object on that class, to create an object, you use the new keyword, followed by the name of the class that you want to base the object on. You can then assign this object to a variable, much like any other value.
The above shows that how to create the two instances of that class named h1, which is two h1 object. $h2 and $h3 both are work as object of the class named h1 and they are based on the same class and have the independent of each other, and each is stored in its own variable.
With use of function named print_r(), you are allowed to output object. In the previous function we have discussed that print_r() function is used to print the output of array. And with the use of this function is very handy for debugging object - oriented code.
Working with Methods When you start adding methods to classes that they become truly powerful. An object then becomes a nicely encapsulated chunk of functionality, containing both data and the methods to work on that data.
Method Visibility In earlier chapter we have studied how to work with three visibility level: public, private and protected. The same is true of methods. All methods can be called by other methods within the same class. If a method is declared public, any code outside the class definition can also potentially call the method.
When your method is declared by private then only other method within the same class can call it. And the method is declared by the protected can called by the other method of the same class or in a class inherits from class.
Creating a Method To add a method to a class, use the public, private, or protected keyword, then the function keyword, followed by the method name, followed by parentheses.
If you forgot to specify the keyword like public, protected and private. If you do these then it will automatically assumed Public.
Calling Method To call the method of the class, simply write the object’s name, then the same arrow used for accessing properties ( - > ), then the method name followed by parentheses:
$object-> method();
Here ’ s a simple example that creates a class with a method, then creates an object from the class and calls the object ’ s method:
Adding parameter and returning values In the method we are allowed to pass the value which will work as argument and also return the value same as the function. For example,
To add parameters, specify the parameter names between the parentheses after the method’s name:
To return a value from a method — or to simply exit a method immediately — use the return keyword:
Now your next task is to create the object on that class, to create an object, you use the new keyword, followed by the name of the class that you want to base the object on. You can then assign this object to a variable, much like any other value.
The above shows that how to create the two instances of that class named h1, which is two h1 object. $h2 and $h3 both are work as object of the class named h1 and they are based on the same class and have the independent of each other, and each is stored in its own variable.
With use of function named print_r(), you are allowed to output object. In the previous function we have discussed that print_r() function is used to print the output of array. And with the use of this function is very handy for debugging object - oriented code.
Working with Methods When you start adding methods to classes that they become truly powerful. An object then becomes a nicely encapsulated chunk of functionality, containing both data and the methods to work on that data.
Method Visibility In earlier chapter we have studied how to work with three visibility level: public, private and protected. The same is true of methods. All methods can be called by other methods within the same class. If a method is declared public, any code outside the class definition can also potentially call the method.
When your method is declared by private then only other method within the same class can call it. And the method is declared by the protected can called by the other method of the same class or in a class inherits from class.
Creating a Method To add a method to a class, use the public, private, or protected keyword, then the function keyword, followed by the method name, followed by parentheses.
If you forgot to specify the keyword like public, protected and private. If you do these then it will automatically assumed Public.
Calling Method To call the method of the class, simply write the object’s name, then the same arrow used for accessing properties ( - > ), then the method name followed by parentheses:
$object-> method();
Here ’ s a simple example that creates a class with a method, then creates an object from the class and calls the object ’ s method:
Adding parameter and returning values In the method we are allowed to pass the value which will work as argument and also return the value same as the function. For example,
To add parameters, specify the parameter names between the parentheses after the method’s name:
To return a value from a method — or to simply exit a method immediately — use the return keyword:





No comments:
Post a Comment