Learning PHP Basics Part 2

2,365 views

Published on

Learn more basic PHP with static objects, annonymous functions/closures, and public, private and protected methods.

Published in: Technology, Health & Medicine
1 Comment
2 Likes
Statistics
Notes
  • really nice video, thanks for the tips..

    http://www.blog-mutuelle.info
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
2,365
On SlideShare
0
From Embeds
0
Number of Embeds
186
Actions
Shares
0
Downloads
47
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Learning PHP Basics Part 2

  1. 1. PHP Basic Part 2 www.prodigyview.com
  2. 2. OverviewObjectiveFor beginners to further their knowledge of PHP byproviding a greater understanding of objects andfunctions.Requirements Understanding of variables, methods and classesEstimated Time8 minutes www.prodigyview.com
  3. 3. Follow Along With A Code Example1. 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
  4. 4. Concepts Covered Static Methods Public Methods Protected Methods Private Methods Anonymous Functions www.prodigyview.com
  5. 5. Static MethodsSo you’ve come to the point after PHP Basics Part 1, that you areready 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 instantiatingthe 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
  6. 6. Static Method Example1. Define a method as Static Creating A Static Method Executing A Static Method 2. Use the ‘::’ to call/execute the method
  7. 7. Public MethodsIn our last slide we created a method and also declared itas public. A method that is ‘public’ means that any otheruser outside the object can call the function. Here is thecode again on declaring a static method. Sets the method as public
  8. 8. Protected MethodsNext up, protected methods. These methods differ frompublic methods in that only another method inside thecurrent object can call this method. Protected methodscan also be called by children classes when they areextended. We will get into extending a class into a minute. Sets the method as protected
  9. 9. Private MethodsAnd finally we get to private methods. Private methodsare like protected methods in that only another methodinside the class can call it. But they differ from protectedmethods in that they cannot to be extended. Sets the method as private
  10. 10. Our Classwww.prodigyview.com
  11. 11. 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 classand give all the public and protected properties of oneclass to another class. Private methods CANNOT begiven to another class.So we have the class ParentObject. Let us give(extend)it’s methods to another class. www.prodigyview.com
  12. 12. Extending ParentObject Calls the protected function of ParentObject www.prodigyview.com
  13. 13. 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 nothave to be actually declared until needed. Anonymousfunctions can also be stored in a variable, which can bestored in an array and passed to functions and methods.Let’s create a simple closure. www.prodigyview.com
  14. 14. Creating and Storing Closure1. Assign the function to a variable 2. Calls the function through the variable www.prodigyview.com
  15. 15. Challenge!To better understand the concepts presented, an optionalchallenge is being presented.Your challenge for this tutorial is to create a class that hasa static protected method. That static method will have ananonymous function that adds two variables and returnsthe value of the variables added.Extended that class to another and through a publicmethod, call the protected one. www.prodigyview.com
  16. 16. Review1. 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
  17. 17. More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com

×