CloudObjects / Blog / Playing with public phpMAE classes in the directory

Playing with public phpMAE classes in the directory

With phpMAE, you can create PHP classes with short snippets of code and deploy them in a scalable serverless environment. However, you can also use phpMAE to share bits of PHP code with the world. Public phpMAE classes get a page in the CloudObjects directory that includes documentation and the ability to execute said code directly through a web-based interface. That is a great way to showcase algorithms and implementation techniques to other developers to teach and inspire them. It may be a small gimmick for now, but with additional features in the pipeline for phpMAE, I’m hoping to eventually make serverless PHP an excellent starting point for new developers. In this article, I’ll show you a few examples of public phpMAE classes and then explain how to create your own.

Playing with Examples

Let’s kick things off with something fun, a small ASCII art generator that I created specifically for this post. ASCII art is a way to visualize things when all you have is text, by repurposing the arrangement of standard characters. At the time of writing, my ASCII art functions class has a single method, getSnailWithSign(), that draws a snail (since I love snails!) holding a sign with the text you provide. You can see two sections when you view the directory page for the llr:AsciiArtFunctions phpMAE class. The upper part lists all the public PHP methods that the class exposes, the lower part shows the full source code. You can expand any of the methods in the list by clicking on it to see its documentation, taken straight from the source code itself, and execute it immediately. Click on getSnailWithSign(string $textForSign), enter something for the textForSign parameter, and hit the Execute in phpMAE button to see the result of the function.

Looking for something more practical? I recently had to implement email parsing in a private phpMAE class to convert a string like Lukas Rosenstock <> into the name and the email part. I decided to delegate that functionality to a separate phpMAE class that I’m using as a dependency. As I published it, you can try out the two functions on the llr:EMailHelperFunctions directory page.

Creating Public phpMAE Classes

The phpMAE launch announcement post includes a complete tutorial on creating and deploying new phpMAE classes, so please check it out if you want to create one. There are two ways to make it public:

If you already know at the time of creation that your class should be public, add the --public option to the command, like this:

phpmae class:create --confjob --public coid://NAMESPACE/NAME/VERSION

If you’ve previously created your class and want to publish it, you need to edit the configuration manually. Set both co:isVisibleTo and co:permitsUsageTo to co:Public. To do so, open the NAME.VERSION.xml file and replace the line

<co:isVisibleTo rdf:resource="coid://" />

with the following:

<co:isVisibleTo rdf:resource="coid://" />
<co:permitsUsageTo rdf:resource="coid://" />

Submit your updated configuration to CloudObjects Core with this command:

cloudobjects confjob NAME.VERSION.xml

Making a class public has two effects. The first is that it gets a public page in the object directory so that everybody can see that class without signing in to CloudObjects. It also exposes the source code on that page (private classes get a private page without the source code). The second effect is that it is no longer required to add an Authorization header when making requests, which is how the Execute in phpMAE button on the directory page can call it.

One recommendation for public phpMAE classes is adding comments for every method in the following format to have them appear in the method list:

 * This is what my method does.
public function myMethod($myParameter) {
    // implementation goes here

You’re next!

That concludes today’s post. I can’t wait to see the code you share. Please drop a comment below, tweet @CloudObjectsIO, or chat me up on Gitter.

by Lukas Rosenstock

Show Disqus Comment Thread
Privacy Notice: Your IP address will be sent to Disqus when you enable comments.