Object Casting in PHP

1 view
Skip to first unread message

Dave Naimish

unread,
Apr 16, 2008, 11:07:32 AM4/16/08
to Php Object Generator
Hi,

I am newbie and recently I started working on PHP.

Problem description:

I have one class called Employee and it has one property called
Salary. I am developing a web service using nuSOAP libarary which will
expose a method to get employee details, i.e. GetEmployeeDetails. This
web method should return object of Employee type.

Now my first query is that how to return customized type using nuSOAP
library?

my code for consuming web service is like this,
<?php
require_once ('lib/nusoap.php');
require ('Employee.php');

$client = new nusoap_client('http://localhost:5050/Projects/
empPayroll/WebService.php');

$emp = new Employee();

$param = array('name' => 'employee name');

$emp = $client->call('GetEmployeeDetails', $param);

echo $message->Salary();
?>

When I execute above file in browser it returns following error:
Fatal error: Call to a member function Salary() on a non-object in D:
\Program Files\wamp\www\Projects\empPayroll\index.php on line 17

My assumption is that when I say
$emp = $client->call('GetEmployeeDetails', $param);
it actually creates a new "emp" variable instead of retaining it as
Employee type of object.

AND THIS IS GETTING NIGHTMARE FOR ME :(

Let me know if anyone of you have any solution for above problem.

Regards,

Dave Naimish

Joel

unread,
Apr 16, 2008, 11:15:37 AM4/16/08
to Php Object Generator
Webservices & objects are a little bit complicated in PHP 4 (since
you're using nuSOAP vs the native phpsoap library, i assume you're on
v4).

Essentially, when you're transferring data to and from a webservice,
the data is serialized. If you have a WSDL definition file, the web
service can also describe how to reconstruct the data on the other
side. Int, strings etc are easy to pass. More complex data types (such
as arrays) require a bit more work on the definition file. Thus, for
objects, it's even more work.

Did you try serializing your object on the webservice and returning a
string, then reconstruct the object using unserialize?

Dave Naimish

unread,
Apr 16, 2008, 11:28:28 AM4/16/08
to Php Object Generator
Hi,

Thanks for your reply,

As I mentioned I am newbie so I would request you give little more
information on php serialization. My need is very simple that
GetEmployeeDetails method should return a object of type Employee.

Is it very complex to achieve in PHP, Is serialization only way to
resolve my problem?

BTW, I am using PHP 5.0 and nuSOAP is client's requirement.

Thanks for bearing my silly questions.

Regards,

Dave Naimish
> > Dave Naimish- Hide quoted text -
>
> - Show quoted text -

Joel

unread,
Apr 16, 2008, 11:49:19 AM4/16/08
to Php Object Generator
I'd say yes, it's pretty much the way to go.

serialize and base64 encode your object on the server
http://www.php.net/serialize
http://www.php.net/base64_encode

then
on the client, decode and eval the string
http://www.php.net/base64_decode
http://www.php.net/eval

Dave Naimish

unread,
Apr 17, 2008, 5:20:13 AM4/17/08
to Php Object Generator
Right now I am not sure what all things I will be able to do but I am
going to start.

I will let you know the update.

Thanks for your help so far.

Regards,

Dave Naimish

On Apr 16, 8:49 pm, Joel <joel...@gmail.com> wrote:
> I'd say yes, it's pretty much the way to go.
>
> serialize and base64 encode your object on the serverhttp://www.php.net/serializehttp://www.php.net/base64_encode
>
> then
> on the client, decode and eval the stringhttp://www.php.net/base64_decodehttp://www.php.net/eval
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages