开发者

Simple PHP question about classes and objects

I'm very new at PHP and have only written smaller easier scripts. I have a script on my server that does what I want it to do, however, at the end of the script I want to run another script on my server.

This other script takes $_POST like this:

$decoded = json_decode($_POST['arrayWithUserAndAvailable'], true);
//does it's thing..

How do I call this script from within my other script?

Do I need 开发者_运维知识库to create an object out of it/class. Don't know how object oriented programming works in PHP.

If anyone can give me some guidance I would be grateful!


Use include to add and parse another PHP file from within your script.

You can prevent your "other file" (the included file) from printing data using this method:

//PHP code
ob_start(); //Start buggering the output
include "otherfile.php";
ob_end_clean(); //Throw away the caught output
// Anything defined in otherfile.php are also defined now.

If $_POST['arrayWithUserAndAvailable'] does not exist in your caller, you might consider editing otherfile.php such that it can take arguments in an alternative way:

//otherfile.php
$decoded = isset($decode) ? $decoded :$_POST['arrayWithUserAndAvailable'];
//additional tests to ensure that $_POST["..."] is sane.
$decoded = json_decode($decoded, true);

//mainfile.php
$decoded = "foo";
include "otherfile.php";


It really depends on the way your code is laid out, and you haven't shown enough of it to be able to tell. "Calling the other script" may be as simple as using an include if it is not already in a class. Otherwise, you may wrap it in a class, put the logic inside of the constructor or some other method, and call as seen in the manual in the index or the basics. If you have a more specific question, elaborating may help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜