![]() |
||||
|
First GlimpseDecember 8, 2004 If you're new here, and if you're not familiar with enterprise-level development concepts, then you're probably wondering what exactly phpBeans are. Either way, an introduction is only proper.The basic concept of phpBeans is a client/server model, where objects that live on the server are accessible to the client via a TCP/IP-based protocol. Calling objects via the phpBeans client API is almost completely transparent, the only difference being the way the objects are instantiated within the client (via the phpBeans API instead of with the usual $instance = new ObjectName () syntax). Similarly on the server-side, creating objects (called 'beans' in phpBeans terminology) is simply a matter of writing an ordinary PHP class, with the only difference being that these classes extend a generic PHP_Bean class, and there are a few simply lines to be copied into the constructor method of each class. With that small of a difference, your PHP applications have instantly gained a host of benefits, including:
Here is a class definition for a real (but rather basic) bean object: <?php
class Bean_Test extends PHP_Bean {
var $values = array ();
function Bean_Test (&$server) {
$this->init ($server);
$this->namespace = 'test';
$this->addMethods (__FILE__);
}
/**
* Sets a named value.
* @access public
* @param string
* @param string
*/
function set ($name, $value) {
$this->values[$name] = $value;
}
/**
* Gets a named value.
* @access public
* @param string
* @return string
*/
function get ($name) {
return $this->values[$name];
}
}
?>
That's all there is to a bean. Install that into a file named Test.php in the obj/ folder of your phpBeans server, restart the server, and you're ready to write some client code to access it. A basic client that sets and then gets a value from the server would be as follows: <?php
include_once ('phpBeans.php');
$client = new PHP_Bean_Client ('localhost', 3843, 2);
if (! $client->connect ()) {
die ($client->error);
}
if (! $client->authenticate ('admin', 'admin')) {
die ($client->error);
}
$test =& $client->getObject ('test');
$test->set ('foo', 'bar');
echo $test->get ('foo');
?>
That's all there is to it. If you run this script, assuming your connection parameters are correct, you should see the text 'bar' printed on the screen. This value was first sent and then retrieved from the remote object on the server, yet notice that the object used on the client-side acts just as if it was an ordinary local object. That's the phpBeans standard at work. So as you can see, phpBeans is a powerful new option available to PHP developers, which has the potential to help usher in a new wave of enterprise-class PHP applications. |
|||
| © 2004 Simian Systems Inc. All rights reserved. Terms of Use || Privacy | ||||
![]() Powered by Sitellite Enterprise PHP CMS |
||||