开发者

Can't return value from another instantiated class/method using WebORB for PHP

I'm trying to use WebORB for PHP.

The /weborb/ folder has been copied into my web root and I can access the console via /weborb/index.php.

I've copied my test application into /weborb/_Services/Test/Main.php. The file contents is as follows:

<?php
require_once '/home/user/www/MyClass.php';

class Main
{
    public funct开发者_开发技巧ion testMethod($str)
    {
        return $this->MyClass->myMethod($str);
    }
}
?>

The file contents of "/home/user/www/MyClass.php" is:

<?php
class MyClass
{
    public function myMethod($str)
    {
        return $str;
    }
}

$MyClass = new MyClass();
?>

When I try to pass a string via the console is just says "Channel disconnected". There's nothing being logged into the error_log either. If I replace:

return $this->MyClass->myMethod($str);

..with..

return $str;

..it works! I simply want to be able to call other instantiated classes/methods.


Problem is that you are not declaring nor instantiating MyClass in your Main class

Try this, it should work.

<?php

require_once '/home/user/www/MyClass.php';

class Main {

    /**
     * 
     * @var MyClass
     */
    protected $_myClass = null;

    /**
     * Default Constructor
     */
    public function __construct() {
        $this->_myClass = new MyClass();
    }

    /**
     * Test Method
     *
     * @param string $str
     * @return string
     */
    public function testMethod($str) {
        return $this->_myClass->myMethod($str);
    }

}

?>

In your MyClass.php file you do not need to create variable $MyClass, it is useless. It will be out of the scope for any other script. Just define the class and that is it, then use this class to create new objects, like in example above.

<?php

//  MyClass.php
// 
//  ONLY Class Definition!!!
//
class MyClass {

    public function myMethod($str) {
        return $str;
    }

}

?>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜