16
Dynamic Objects

Creating Dynamic Objects PHP

Embed Size (px)

DESCRIPTION

Learn how to create dynamic objects that you can add methods and variables too on the fly

Citation preview

Page 1: Creating Dynamic Objects PHP

Dynamic Objects

Page 2: Creating Dynamic Objects PHP

OverviewObjective

Learn how to create dynamic objects that you can add methods and variables too on the fly.

Requirements

Understanding of PVCollections

Understanding of Closures/Anonymous Functions

Estimated Time

10 minutes

www.prodigyview.com

Page 3: Creating Dynamic Objects PHP

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/design/Objects.php

Page 4: Creating Dynamic Objects PHP

The ObjectIn ProdigyView, PVObject and PVStaticObject are very powerful classes.

Have the design patterns to improve development

Enable Object Oriented Programming

Enable Aspect Oriented Programming

Easily mutable code

Before we get into the higher level constructs, lets start with understanding PVObjects and PVStaticObjects.

Page 5: Creating Dynamic Objects PHP

Create Our ClassWe can start learning about how to use an object by creating an empty class. Yes, just merely define a class with nothing in it.

Extend PVObject or PVStaticObject

Take notice it’s empty, no methods or variables

Page 6: Creating Dynamic Objects PHP

Anonymous functions/closures

Next step is to create our anonymous functions . These functions are eventually going to become the class’s methods.

Page 7: Creating Dynamic Objects PHP

Adding the MethodsWe have a class with no methods and we have two anonymous functions. Ok we are ready to go. Because our class extends PVObject, we have access to a method called addMethod(). This method will allow us to add our anonymous functions as methods that become part of the class.Set the name of the method

Add our anonymous function

Page 8: Creating Dynamic Objects PHP

Calling the MethodsOk so we’ve added the methods to our object that was once empty. Now we can call those methods by using the -> operator and the name we assigned the method.

Remember, the anonymous function $write_email accepted two parameters.

Name of the method

Page 9: Creating Dynamic Objects PHP

PVCollectionIf you remember PVCollection, we could add information and retrieve information from it at any time.

Every class that extends PVObject and PVStaticObject has a PVCollection built in. This makes it possible to add data to an object and also iterate through the objects data.

Let’s take a look on the next slide.

Page 10: Creating Dynamic Objects PHP

Adding VariablesWhen we created our class, it was empty with no variables. Let’s change that by adding some variables. The method addToCollectionWithName is a method inherited from PVObject and will allow us to add variables to the objects collection that can be retrieved later. Or you can explicitly add a variable by assigning it using the ‘->’ operator.

Name of the variable Value associated with the name

Name of the variable Value associated with the variable

Page 11: Creating Dynamic Objects PHP

Get the variablesLike the methods, we can now get the variables that were assigned to our once empty object. Also we can iterate through the variables that were added.

Get the variables by calling the name assigned to them

Page 12: Creating Dynamic Objects PHP

PVObjects

Pretty cool? Well this is only the tip of the ice berg.

With a clear understanding of adding methods and variables to an object, we have the ability to create very dynamic objects.

Once combined with design patterns, you will able to leverage aspect oriented programming and other capabilities that will make building an application incredibly easy and robust.

Page 13: Creating Dynamic Objects PHP

Challenge!

To better understand the concepts covered, complete the optional challenge below.

1. Start out with an empty class.

2. Add a variable to the class

3. Add a function to this call that will retrieve the variable and pass it to another function that alters the result.

4. Then pass the altered result back to where the function was originally called.

Page 14: Creating Dynamic Objects PHP

The Not So Obvious1. Adding a variable to an object can also be

accomplish by using the method addToCollection. But the variable will not be assigned a key for accessing it later. The variable can retrieved when using the getIterator() method.

2. All objects have a PVCollection. All variables are assigned and pulled from this collection behind the scenes.

Page 15: Creating Dynamic Objects PHP

ReviewTo create dynamic objects, make sure the class

extends PVObject or PVStaticObject

Closures/anonymous functions can be added to using the addMethod function

Variables can be added using the addCollectionWithName() method or explicitly applying the variable with ‘->’ operator.

Page 16: Creating Dynamic Objects PHP

API ReferenceFor a better understanding of the Collections and the Iterator, check out the api at the two links below.

PVStaticObject

PVObject

www.prodigyview.com

More Tutorials

For more tutorials, please visit:

http://www.prodigyview.com/tutorials