PDA

View Full Version : Including other files within AMFPHP Service Classes


FFighter
06-07-2005, 04:49 PM
What I want to do is something very simple - include other php in a PHP class used by AMFPHP as a service for the Flash Remoting so I can use the resources in my service class.

E.g: I need to use the phpAdsNew in my flash website. So I did a little modification to the code and then created a Ads.php service. The file looks like this:


include "../system/Ads/phpadsnew.inc.php";

class Ads() {

function Ads() {
$this->methodTable = array(
"getBanner" => array(
"description" => "Pega um banner do sistema PHPAdsNew",
"access" => "remote",
"arguments" => array("what")
)
);

}

function getBanner($what) {
//does not work:
$arr = view_raw($what);
return $arr; //it does not return anything to flash... :/

}


When I take out the "$arr = view_raw($what)" line and change the return to return "SOMETHING" for test purposes, it works, the string is sent to the server :confused: what could I do to bypass/fix it?

Vegeta
07-13-2005, 05:13 AM
Hi

I was facing the same problem today and I found a solution. AMFPHP creates the service objects dynamically by inspecting the path you configure on it and trying to find a class inside a file with the same name of the service you call, then (I am guessing here) it creates an object of that class dynamically (maybe eval, maybe $obj = new $class(), don't know yet) but somehow global variables declared in included files don't survive the creation, so the only way to access those variables is to include the necessary files inside the service class, wether in the constructor or inside the functions you need, take this code for example:


<?php

class RegistroFlash {
var $registro;

function RegistroFlash(){
include_once 'servicioRegistro.php';
include_once 'config.php';
$this->registro = &new ServicioRegistro($config);

$this->methodTable = array(
"entrada" => array(
"description" => "something",
"access" => "remote",
"arguments" => array ("arg1","arg2")
),
// more code
?>


The ServicioRegistro object depends in a global array called $config that lives inside config.php but if I just include the file before the class declaration, the variable never gets created, or if it does its beyond AMFPHP's reach (out of scope)

So, my recommendation for these kind of service objects is to include your dependencies inside the class. It works for me.

Good luck