learning php basics part 2
DESCRIPTION
Learn more basic PHP with static objects, annonymous functions/closures, and public, private and protected methods.TRANSCRIPT
OverviewObjective
For beginners to further their knowledge of PHP by providing a greater understanding of objects and functions.
Requirements
Understanding of variables, methods and classes
Estimated Time
8 minutes
www.prodigyview.com
Follow Along With A Code Example
1. Download a copy of the example code at www.prodigyview.com/source.
2.Install the system in an environment you feel comfortable testing in.
3.Proceed to examples/basics/PHP_Basics.php
http://www.prodigyview.com
Concepts CoveredStatic Methods
Public Methods
Protected Methods
Private Methods
Anonymous Functions
www.prodigyview.com
Static MethodsSo you’ve come to the point after PHP Basics Part 1, that you are ready to dive a little deeper. Who am I to hold you back?
If you remember from part 1, we worked with objects with methods. Now we are going to work with objects that have static methods.
What is a static method?
A static method is a method that can be called without instantiating the object. Simply put, we can access the method without using ‘new Object()’. Static methods are accessed by using this syntax: ‘class_name ::method_name’ .
www.prodigyview.com
Static Method Example
Creating A Static Method
Executing A Static Method
1. Define a method as Static
2. Use the ‘::’ to call/execute the method
Public Methods
In our last slide we created a method and also declared it as public. A method that is ‘public’ means that any other user outside the object can call the function. Here is the code again on declaring a static method.
Sets the method as public
Protected MethodsNext up, protected methods. These methods differ from public methods in that only another method inside the current object can call this method. Protected methods can also be called by children classes when they are extended. We will get into extending a class into a minute.
Sets the method as protected
Private MethodsAnd finally we get to private methods. Private methods are like protected methods in that only another method inside the class can call it. But they differ from protected methods in that they cannot to be extended.
Sets the method as private
Extending A ClassSo far it’s been mentioned twice about extending a class.
What does this mean?
Extending a class mean that you can create another class and give all the public and protected properties of one class to another class. Private methods CANNOT be given to another class.
So we have the class ParentObject. Let us give(extend) it’s methods to another class.
www.prodigyview.com
Extending ParentObject
Calls the protected function of ParentObject
www.prodigyview.com
Anonymous FunctionsFinally for this tutorial we have anonymous functions, which are also known as closures.
These are functions that be created on the fly and do not have to be actually declared until needed. Anonymous functions can also be stored in a variable, which can be stored in an array and passed to functions and methods. Let’s create a simple closure.
www.prodigyview.com
Creating and Storing Closure
1. Assign the function to a variable
2. Calls the function through the variablewww.prodigyview.com
Challenge!To better understand the concepts presented, an optional challenge is being presented.
Your challenge for this tutorial is to create a class that has a static protected method. That static method will have an anonymous function that adds two variables and returns the value of the variables added.
Extended that class to another and through a public method, call the protected one.
www.prodigyview.com
Review
1. Static methods are accessed with ‘::’ operator.
2. Objects do not have to be instantiated to use a static method.
3. Public methods are accessible by everyone.
4. Protected methods are also accessible to other methods within the object and children classes.
5. Private methods are only accessible to methods within the current class.
6. Closures/Anonymous Functions can be assigned to a variable.
www.prodigyview.com
www.prodigyview.com
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials