php basics part 2

16
PHP Basic Part 2 www.prodigyview.com 

Upload: prodigyview

Post on 06-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 1/16

PHP Basic Part 2

www.prodigyview.com 

Page 2: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 2/16

OverviewObjective

For beginners to further their knowledge of PHP byproviding a greater understanding of objects and

functions.

Requirements

Understanding of variables, methods and classes

Estimated Time

8 minutes

www.prodigyview.com 

Page 3: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 3/16

Follow Along With A CodeExample

1. Download a copy of the example code atwww.prodigyview.com/source.

2.Install the system in an environment you feelcomfortable testing in.

3.Proceed to examples/basics/PHP_Basics.php

http://www.prodigyview.com 

Page 4: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 4/16

Concepts CoveredStatic Methods

Public Methods

Protected Methods

Private Methods

Anonymous Functions

www.prodigyview.com 

Page 5: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 5/16

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 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 

Page 6: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 6/16

Static Method Example

Creating A Static Method

Executing A Static Method

Define a method as Static

Use the ‘::’ to call/execute the method 

Page 7: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 7/16

Public Methods

In our last slide we created a method and also declared itas public. A method that is ‘public’ means that any other 

user outside the object can call the function. Here is thecode again on declaring a static method.

Sets the method as public

Page 8: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 8/16

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 are

extended. We will get into extending a class into a minute.

Sets the method as protected

Page 9: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 9/16

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

Page 10: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 10/16

Our Class

www.prodigyview.com 

Page 11: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 11/16

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 

Page 12: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 12/16

Extending ParentObject

Calls the protected function of ParentObject

www.prodigyview.com 

Page 13: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 13/16

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. 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 

Page 14: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 14/16

Creating and Storing ClosureAssign the function to a variable

Calls the function through the variable

www.prodigyview.com 

Page 15: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 15/16

Challenge!To better understand the concepts presented, an optionalchallenge is being presented.

Your challenge for this tutorial is to create a class that has

a 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 public

method, call the protected one.

www.prodigyview.com 

Page 16: PHP Basics Part 2

8/3/2019 PHP Basics Part 2

http://slidepdf.com/reader/full/php-basics-part-2 16/16

Review

1. Static methods are accessed with ‘::’ operator. 

2. Objects do not have to be instantiated to use a staticmethod.

3. Public methods are accessible by everyone.

4. Protected methods are also accessible to other methodswithin the object and children classes.

5. Private methods are only accessible to methods within thecurrent class.

6. Closures/Anonymous Functions can be assigned to avariable.

www.prodigyview.com