472,146 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

SOAP question - creating instances/ accessing constructor method?

I have a class that I want to make accessible to a web service. This
class does some work in its constructor method and sets some class
variables in its various methods. The problem I am having is creating
an instance of this class when it is called via SOAP. I don't seem to
have access to the constructor method or any class variables... is this
by design? Must all methods be static? Here is my sample code:

This is a very dumbed down sample of the class. Please don't say it
would be easier to just use static methods, the actual class is far
more complex.

Class User {
public $user_id;
public $email;

public function __construct($user_id) {
$this->user_id = $user_id;
$this->email = $this->get_user_email($user_id)
}

public function get_user_email($user_id) {
//blah blah, do some work and return email
}
}

I am currently accessing this class using:

$user = new SoapClient("http://localhost/ws/user.wsdl");

And I can get this to work to call any static methods in the class,
however I would like to be able to create the user object and then
access $user->email. Is this possible?

Aug 31 '05 #1
8 3197
Piro wrote:
I have a class that I want to make accessible to a web service. This
class does some work in its constructor method and sets some class
variables in its various methods. The problem I am having is creating
an instance of this class when it is called via SOAP. I don't seem to
have access to the constructor method or any class variables... is
this by design? Must all methods be static? Here is my sample code:


A requirement to get this to work is that your soap server supports
persistency so it's able to keep track of the class instance during the
execution of the client. Persistency can be enforced with the
SoapServer->setPersistence() method.

Below is an example implementation:

Server:

<?php

class User {
public $name;

public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}
}

session_start();
$server = new SoapServer(null, array('uri' => 'http://localhost/'));
$server->setClass('User');
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->handle();

?>

Client:

<?php

$client = new SoapClient(
null,
array(
'location' => 'http://localhost/server.php',
'uri' => 'http://localhost/',
'trace' => 1
)
);

$client->setName('John');
print $client->getName();

?>
JW

Aug 31 '05 #2
Janwillem Borleffs wrote:
session_start();
$server = new SoapServer(null, array('uri' => 'http://localhost/'));
$server->setClass('User');
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->handle();

You can omit the call to the session_start() function, which isn't required.
$client = new SoapClient(
null,
array(
'location' => 'http://localhost/server.php',
'uri' => 'http://localhost/',
'trace' => 1
)
);


The trace option here is only required when you want to debug the soap
request/response with the designated functions.
JW

Aug 31 '05 #3
thanks, I'll try that. I notice your client call doesn't use WSDL, is
there a reason to do it that way?

Also, am I still correct that the constructor method is not accessible
in the soap call?

Aug 31 '05 #4
Piro wrote:
thanks, I'll try that. I notice your client call doesn't use WSDL, is
there a reason to do it that way?

No, I'm just lazy
Also, am I still correct that the constructor method is not accessible
in the soap call?


Although the SoapServer->setClass() method accepts additional arguments
which will get passed to the class constructor, there doesn't seem to be an
elegant way of using this. With that in mind, the only straightforward way
to assign class properties is to implement setter methods.
JW

Aug 31 '05 #5
Also, I noticed you wrote a method to return a class property... is
there any way to access the property directly? Rather than

print $client->getName();

I was thinking

print $client->name;

It seems cumbersome to have methods for each class property, especially
if there are a bunch of them.

Aug 31 '05 #6
Piro wrote:
Also, I noticed you wrote a method to return a class property... is
there any way to access the property directly? Rather than

print $client->getName();

I was thinking

print $client->name;

It seems cumbersome to have methods for each class property, especially
if there are a bunch of them.


Perhaps, but in general it's considered bad practise to access class
properly without a getter, unless they are defined as public statics.

Anyways, when you don't want to define setters, you can use overloading
by adding the following method to your class:

public function __call($function, $arguments) {
$function = strtolower($function);
$var = preg_replace('/^get/', '', $function);
if (isset($this->$var)) {
return $this->$var;
}
}

This way you can use method calls like $client->getName() to get a
property with the name $name, $client->getAnother_name() to get a
property with the name $another_name etcetera.

And, as you have might have guessed by now, there isn't a way to access
teh class properties directly as setClass() only exports class methods.
JW

Sep 1 '05 #7
On Wed, 31 Aug 2005 16:47:01 -0700, Piro wrote:
Also, I noticed you wrote a method to return a class property... is
there any way to access the property directly? Rather than

print $client->getName();

I was thinking

print $client->name;

It seems cumbersome to have methods for each class property, especially
if there are a bunch of them.


....maybe now is a good time to do some reading on Object Orientated
Programming? And the fact that the primary concept is to *never* directly
access the data that the methods are protecting?

Steve

Sep 1 '05 #8
Thanks... setting the persistence did the trick. I just created a
"setup" method that I call to replace the functionality of the
constructor.

Sep 1 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Sham Ramakrishnan | last post: by
4 posts views Thread by Pete Wittig | last post: by
6 posts views Thread by Peter van der veen | last post: by
3 posts views Thread by =?Utf-8?B?SGVtaWw=?= | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.