02
Sat, Nov
2 New Articles

New Zend Toolkit: Part 2

Web Languages
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Now, let's look at defining parms and calling programs.

 

With the excitement of the holidays over, it's now time to continue with our review of how to call an RPG program from within a PHP script. We had started this two columns ago by looking at how to do this using the old Aura/Easycom toolkit that had been incorporated into Zend Server prior to release 5.6. That toolkit was procedural in nature, so it was pretty intuitive to look at. My last column got us into the new, true Zend toolkit, which is object-oriented and so may look a bit strange to many of us.

What We've Covered So Far

In that last exciting episode, we got our hands wet with the first part of the code that's required in a PHP script to access an RPG program. And, if you have remembered anything through the holiday season, you know that this first portion consisted of two basic tasks.

  1. First, bringing the toolkit code into your script via the require_once operator so that the necessary classes and such are available to you
  2. Second, setting up a variable, in this case one that we called $conn, that would contain an instance of the ToolkitService class and specifically the ToolkitServiceParams variable

require_once 'ToolkitService.php';

$conn = ToolkitService::getInstance('*LOCAL', 'MYUSER', 'MYPASS');

$conn->setToolkitServiceParams(array('stateless'=>true));

If I thought the above looked too weird, I would be tempted to go back and re-read the previous installment. 'Course, I would also be tempted to keep going in the hope that somehow it would all suddenly make sense, so I guess it's up to you. So much of "understanding" something is just recognizing familiar patterns. That is, when a procedural programmer sees Total$ = Total$ + Order$, they know that this is really an evaluate statement where we're adding the value of this particular order to a field that is going to carry the value of all orders being looked at, and it seems patently obvious. Same thing is true of the junk above. It all makes sense if you understand it.

The question is, what comes next? And the answer is…

Defining the Parms to Be Passed to the RPG Program

We'll start by setting two variables, one to '1' and the other to blanks. Note that there's no indication how long these are or what type they are. This is an implicit definition: indeterminate length, data type alphanumeric. PHP does allow us to be more specific using the casting feature, but we don't need to in this case, so why bother? We'll see in a moment that these are the parameters that will be passed to the RPG program.

 

$code =  '1';

$desc = '';

The next two statements look kind of weird, but they're also defining parameters that will go with the call to the RPG program. Why are there two of them? Because we're using a call in this example script that passes two parms to the RPG program. The first thing you notice is that the parm is not $parm, which would refer to a single parameter value, but rather $parm[], which is an array. Each of these statements then sets a parameter in the array $parm[].

And the $conn->? This means, as it did in part 1 of this article, that we want to get an instance of ToolkitService, specifically the AddParameterChar, so that we can use that in the statement.

Finally, the AddParameterChar is a method (an object-oriented function) within ToolkitService that defines a parameter. It (the method) has five parameters associated with it (input/output direction, parm size, a spot for a developer comment, the parm name, and the parm value). In our case, we're going to be passing two parms in the $parm[] array to the RPG program: a 10-digit 'code' field whose value will be in the $code variable we defined above, and a 10-digit 'description' field where the value comes from the $desc variable. And, since the direction value for both is 'both', this parm is not only passed, but passed back to the script.

$param[] = $conn->AddParameterChar('both', 10, 'CODE', 'CODE', $code);

$param[] = $conn->AddParameterChar('both', 10, 'DESC', 'DESC', $desc);

See how everything is really coming together here? Man, we're rollin' now, baby.

Calling the RPG Program

In the next step, we finally get around to calling the RPG program that we need. And we do this similar to the way we call a program in ILE using the EVAL command rather than the CALLP. That is, we set a variable, in this case $result, and use the $conn-> to access the method PgmCall. We pass four parameters to the PgmCall method: the program name of what we are calling, the library in the i where this is being kept, the parms that are being sent, the program return value (set to null in our case), and the service program name (if we're dealing with a service program, which in this case we aren't).

$result = $conn->PgmCall("COMMONPGM", "ZENDSVR", $param, null, null);

Evaluating the Result of the Call

It would be nice if that was all there was to it, but once the program has been called, then eventually it will be done and control will return to the PHP script, and we want to know two things. First, did the call work? That is, did we find the RPG program we were calling and did it kick off? And second, what was the output of that program? Since we'll tend to use the RPG programs to edit and access data, it's only logical that data should be returned from it.

f($result) {   

    $outputParams = $result['io_param'];   

   echo "Code: {$outputParams['CODE']}. Desc: {$outputParams['DESC']} ";

               } else {    echo "Error calling COMMONPGM.";}

This code is fairly common for checking the results of a call, and a similar format would be used whether you had called an RPG program using the toolkit or had just issued a more standard call to SQL to retrieve some data. Basically, we're doing an if/else statement where if something is returned in the $result variable, we perform the top half of the if statement, and if nothing was returned, we know the call failed and we execute the stuff after the else.

What makes it confusing is the braces ({}), which are used to enclose the if and the else parts of the statement. Within the braces, you can have multiple PHP statements, each one delimited by the semi-colon. Remember that every time you see a semicolon you have come to the end of a PHP statement. In this case, if you receive results from your call, they're moved to the variable $outputParams and a message (the echo command) is displayed on your page. If things don't go so well, then an error message is displayed on the page.

What's important is not what's specified in this example. If things go well, you may not want to display a message; you might just want to set the returned parms so they show up on the page and call it good. It's up to you.

What Does It All Mean?

As you can see, there are a number of things to consider.


First, it's my hope that, after looking at the object-oriented code in these two articles, you have come away with a small bit of understanding but mostly a feeling that "OO ain't so bad." It really isn't bad, just different. And you might as well start getting used to it because it definitely will ratchet your skills up to the next level.

Second, whether you have to use the new OO toolkit or the old procedural one depends on what version of the Zend Server you're on. If you're prior to 5.6 (or have upgraded to 5.6 from a prior release), then you have the old toolkit. If you came in on Zend Server 5.6 or above, then you have the OO one.

And third, and maybe most importantly, whether you have to use it at all depends on how you're going to structure your applications. Remember, PHP is not a substitute for RPG. You will be using them both, and the question then becomes which one will be the control language. That is, in order to interweave the two together, you'll need to use some sort of modular programming approach rather than just a big ol' RPG program (BORP), probably something like the MVC model. The question then becomes which language you'll use for the controller part. Will PHP be your main language, from which you call RPG programs that will retrieve, maul, and edit data before sending it to PHP, or will you use RPG to control what is going on and call PHP only when it's time to build a page? With the former, you'll need to use the toolkit; with the latter, you can simply call your PHP scripts from RPG using QShell and the PHP Command Line Interface technique.

The bad part is it forces you to make some decisions before you start your design. The good part is there's a lot of alternatives to choose from. Or maybe it's vice versa.

David Shirey

David Shirey is president of Shirey Consulting Services, providing technical and business consulting services for the IBM i world. Among the services provided are IBM i technical support, including application design and programming services, ERP installation and support, and EDI setup and maintenance. With experience in a wide range of industries (food and beverage to electronics to hard manufacturing to drugs--the legal kind--to medical devices to fulfillment houses) and a wide range of business sizes served (from very large, like Fresh Express, to much smaller, like Labconco), SCS has the knowledge and experience to assist with your technical or business issues. You may contact Dave by email at This email address is being protected from spambots. You need JavaScript enabled to view it. or by phone at (616) 304-2466.


MC Press books written by David Shirey available now on the MC Press Bookstore.

21st Century RPG: /Free, ILE, and MVC 21st Century RPG: /Free, ILE, and MVC
Boost your productivity, modernize your applications, and upgrade your skills with these powerful coding methods.
List Price $69.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: