| |
Returning objects from bean methods
Forum /
Server Programming /
Returning objects from bean methods
Reply
Subscribe
Start new thread
Syndicated Feed (RSS)
|
Displaying 1 to 2 of 2
|
Previous
1
Next |
| Author |
Message |
afranco
Posts: 1
|
|
Returning objects from bean methods - Posted: April 19, 2005 - 1:34 PM
|
Quote and reply
|
Hello there and thanks for your work on this great software.
I have a large (>400 objects) class-library/framework [ http://harmoni.sf.net ] of common services [ http://www.phpoki.org ] that I would like to make available to applications running on multiple/remote servers. phpBeans seems like it may be the ideal technology for this endeavor, but the documentation is a little thin in a few places. :-)
I have successfully created a "Loader" bean which includes, instantiates, and configures the framework and its various "services". These services seem to all happily run in the beans-server when they are accessed from this Loader (or other beans).
The Question:
My problem arises in that most of the return values of the various services are objects which need to tie back into the beans-server environment for their method executions. I have yet to determine the proper way of passing a reference to a bean (or other, non-bean object on the beans-server) as the return value of a bean's method.
My attempts so far:
To start off, I am attempting to provide access to Harmoni's IdManager, a service for creating unique Ids.
IdManager code listing: http://harmoni.sourceforge.net/harmoniDoc/phpdoc/__filesource/fsource_harmoni_osid_v2.id_harmonicoreoki2idHarmoniIdManager.class.php.html
Id code listing:
http://harmoni.sourceforge.net/harmoniDoc/phpdoc/__filesource/fsource_harmoni_osid_v2.shared_harmonicoreoki2sharedHarmoniId.class.php.html
In my attempt to provide access to the IdManager, I have created three Beans:
- Loader: Sets up and configures the framework.
- IdManager: A wrapper for the IdManager service in the framework.
- Id: A wrapper for the Id class in the framework.
The IdManager and the Id are shown below. Please note that this is an extremely simple service (my reason for starting with it). The Id object is primarily a "data-object" that does not really rely on the environment of the beans-server. Other framework services [ http://harmoni.sourceforge.net/harmoniDoc/phpdoc/ ], such as Authorization [ http://harmoni.sourceforge.net/harmoniDoc/phpdoc/harmoni/osid_v2.authorization/HarmoniAuthorizationManager.html ] and Digital Repository [ http://harmoni.sourceforge.net/harmoniDoc/phpdoc/harmoni/osid_v2.repository/HarmoniRepository.html ] need to return objects to the client that can map-to/call-methods-on complex object trees that live in the beans-server.
In the Bean_IdManager::createId() method the Bean_Id object seems to be instantiated properly before it is returned. Changing the implementation of createdId() to the following:
<?php
function &createId () {
$idManager =& Services::getService("IdManager");
$id =& new Bean_Id($this->_server, $idManager->createId());
return $id->getIdString();
}
?>
returns an appropriate string. When the Id itself is returned (as shown in the listing below) the client script only receives "false" instead of an object.
IdManager.php:
<?php
/**
* IdManager creates and gets Ids. Ids are used in many different contexts
* throughout the OSIDs. As with other Managers, use the OsidLoader to load
* an implementation of this interface.
*
* <p>
* All implementations of OsidManager (manager) provide methods for accessing
* and manipulating the various objects defined in the OSID package. A manager
* defines an implementation of an OSID. All other OSID objects come either
* directly or indirectly from the manager. New instances of the OSID objects
* are created either directly or indirectly by the manager. Because the OSID
* objects are defined using interfaces, create methods must be used instead
* of the new operator to create instances of the OSID objects. Create methods
* are used both to instantiate and persist OSID objects. Using the
* OsidManager class to define an OSID's implementation allows the application
* to change OSID implementations by changing the OsidManager package name
* used to load an implementation. Applications developed using managers
* permit OSID implementation substitution without changing the application
* source code. As with all managers, use the OsidLoader to load an
* implementation of this interface.
* </p>
*
* <p>
* Unlike most Managers, IdManager does not have methods to return Type
* information.
* </p>
*
*
* <p>
* OSID Version: 2.0
* </p>
*
* @package harmoni.osid_v2.id
*
* @copyright Copyright &copy; 2005, Middlebury College
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
*
* @version $Id: HarmoniIdManager.class.php,v 1.15 2005/04/04 18:23:50 adamfranco Exp $
*/
class Bean_IdManager
extends PHP_Bean
{
function Bean_IdManager(&$server) {
$this->init ($server);
$this->namespace = 'IdManager';
$this->addMethods (__FILE__);
$this->_server =& $server;
}
/**
* Create a new unique identifier.
*
* @return object Bean_Id
*
* @access public
*/
function &createId () {
$idManager =& Services::getService("IdManager");
$id =& new Bean_Id($this->_server, $idManager->createId());
return $id;
}
/**
* Get the unique Id with this String representation or create a new unique
* Id with this representation.
*
* @param string $idString
*
* @return object Bean_Id
*
* @access public
*/
function &getId ( $idString ) {
return new Bean_Id($this->_server, $idManager->getId($idString));
}
}
?>
Id.php:
<?php
/**
* Id represents a unique identifier. A String representation of the unique
* identifier is available with getIdString(). To convert from a String
* representation of the identifier to the identifier object,
* org.osid.shared.Id, use getId(String). Id can determine if it is equal to
* another Id.
*
* <p>
* OSID Version: 2.0
* </p>
*
* @package harmoni.osid_v2.shared
*
* @copyright Copyright &copy; 2005, Middlebury College
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
*
* @version $Id: HarmoniId.class.php,v 1.6 2005/03/29 19:44:29 adamfranco Exp $
*/
class Bean_Id
extends PHP_Bean
{
function Bean_Id(&$server, &$sourceId) {
$this->init ($server);
// $this->namespace = 'Id';
$this->addMethods (__FILE__);
$this->_sourceId =& $sourceId;
}
/**
* Return the String representation of this unique Id.
*
* @return string
*
* @throws object SharedException An exception with one of the
* following messages defined in org.osid.shared.SharedException
* may be thrown: {@link
* org.osid.shared.SharedException#UNKNOWN_TYPE UNKNOWN_TYPE},
* {@link org.osid.shared.SharedException#PERMISSION_DENIED
* PERMISSION_DENIED}, {@link
* org.osid.shared.SharedException#CONFIGURATION_ERROR
* CONFIGURATION_ERROR}, {@link
* org.osid.shared.SharedException#UNIMPLEMENTED UNIMPLEMENTED}
*
* @access public
*/
function getIdString () {
return $this->_sourceId->getIdString();
}
/**
* Tests if an unique Id equals this unique Id.
*
* @param object Id $id
*
* @return boolean
*
* @throws object SharedException An exception with one of the
* following messages defined in org.osid.shared.SharedException
* may be thrown: {@link
* org.osid.shared.SharedException#UNKNOWN_TYPE UNKNOWN_TYPE},
* {@link org.osid.shared.SharedException#PERMISSION_DENIED
* PERMISSION_DENIED}, {@link
* org.osid.shared.SharedException#CONFIGURATION_ERROR
* CONFIGURATION_ERROR}, {@link
* org.osid.shared.SharedException#UNIMPLEMENTED UNIMPLEMENTED},
* {@link org.osid.shared.SharedException#NULL_ARGUMENT
* NULL_ARGUMENT}
*
* @access public
*/
function isEqual ( &$id ) {
return $this->_sourceId->isEqual($id);
}
}
?>
(the forum seems to munge the ampersands a bit, but you get the idea.)
Thanks for your help,
Adam
|
|
Back to top
|
|
lux
Posts: 17
|
Interesting idea, but not one phpBeans solves without some changes. The limitations are that an object on the client-side that gets unserialized won't be "linked" to the object on the server-side, and the class must exist on the client as well or you end up with a generic StdClass object instead. Basically, a data structure and no methods.
I can see something like the following being done on the server:
return new PHP_Bean_Server_Object ('obj_name');
The client would then see that the object type was 'PHP_Bean_Server_Object, call $client->getObject() with the appropriate $obj_name. I don't think it would really work to pass an instance of the actual server object to the client, because of the limitations described above.
The downside of this is that what gets returned is a generic object, no different than having simply called the following from the client's perspective:
$obj =& $client->getObject ('obj_name');
Hopefully this helps a bit, but you'll probably have to change how the API works to accommodate the remote object limitations.
Lux
|
|
Back to top
|
|
Return to Top
|
|