473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 3300
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Guest | last post by:
Hi, I unerstand that if you choose IIS to host your .Net Remotingcomponents with HTTP channel and SOAP formatter, you get thebuilt-in security and configuraion features of IIS. Also we canexpose it...
1
by: Eirik Brattbakk | last post by:
Hi I have some problems accessing a soap service made in c# using an ATL/MFC client over SSL. I have tried both CSoapMSXMLInetClient and CSoapWininetClient as template arguments with my stub...
0
by: Sham Ramakrishnan | last post by:
guys... I have a SOAP service at my clients that I have to use to call the exposed methods... well... there are supposedly 3 ways in which I can look at doing it... 1. Create a proxy for the...
4
by: Kaush | last post by:
Hi all, I am creating a webservice to accept SOAP messages, parse the message and send a SOAP response back to the client accessing my web service using WSE-2 in ASP.NET. I am creating a class...
4
by: Pete Wittig | last post by:
Hello, I am creating an app using Serviced Components and I am exposing them using the Enterprise Services SOAP service. Here is my problem: I have one COM+ application with several serviced...
6
by: Peter van der veen | last post by:
Hi I have the following problem. I'm calling a webservice from within a VB.net 2005 Windows program. For this i got a WSDL file and loaded that in VB. Until now i just call the webservice and...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: =?Utf-8?B?SGVtaWw=?= | last post by:
Hi, I have written a web service for accessing data from a database. I have a method in the webservice which returns a dataset. I am trying to implement error handling by using the...
1
by: CodeSeeker | last post by:
I have an application, which uses pop3 to read the messages from the mailbox, and it has been working fine for so many year. We recently have started changing this application to use java mail IMAP 4...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.