timothee Posted May 19, 2006 Share Posted May 19, 2006 Hi all, I'm trying to create a webservice by using a class but I have some problems with sharing redundant code. I thought I could create private methods in my class to share the common code without exposing the function in the webservice but that is not working :(.Is there a way to achieve this? Here is my sample webservice class. The method, I'd like to share is "connect".[code]<?phprequire_once('DB.php');require_once($_SERVER["DOCUMENT_ROOT"]."/page_tracker/config.inc.php");/** * retrieve hit count for pages, or IDs, over specified period of time */class PageTrackerWS { private $connected = false; private $db; /** * Initializes connection to the DB * @return void */ private function connect() { if ($connected) return; global $config; $db = DB::Connect($config['dsn']); if (DB::isError($db)) { throw new Exception($db->getUserInfo()); } $db->setFetchMode(DB_FETCHMODE_ASSOC); $connected = true; } /** * Returns a list of all page that are marked for tracking * @return string[] */ public function getTrackedPages() { try { connect(); $db->setFetchMode(DB_FETCHMODE_ORDERED); $req = $db->query("SELECT URI from URIS"); if (DB::isError($req)) throw new Exception($req->getUserInfo()); while ($row = $req->fetchRow()) { $ret[] = $row[0]; } return $ret; } catch(Exception $e) { // !! TODO // return Fault here } } /** * Returns the internal ID of a tracked page * @param string $uri The uri * @return string */ public function getPageID($uri) { try { connect(); $ID = $db->getOne("SELECT ID from URIS WHERE URI=?", array($uri)); if (DB::isError($ID)) throw new Exception($ID->getUserInfo()); return $ID; } catch(Exception $e) { // !! TODO // return Fault here } }}?>[/code]Here is the code I use to test my service[code]$client = new SoapClient("http://localhost/wshelper/service.php?class=PageTrackerWS&wsdl");$pages = $client->getTrackedPages();foreach ($pages as $page) echo $page."<br/>";[/code]And the error I get:[code]Fatal error: Uncaught SoapFault exception: [SOAP-ENV:Server] Call to undefined function connect() in /var/www/localhost/htdocs/page_tracker/WSTest.php:14 Stack trace:#0 [internal function]: SoapClient->__call('getTrackedPages', Array) #1 /var/www/localhost/htdocs/page_tracker/WSTest.php(14): SoapClient->getTrackedPages() #2 {main} thrown in /var/www/localhost/htdocs/page_tracker/WSTest.php on line 14[/code]To assist me in managing my Web Service, I am using the [a href=\"http://jool.nl/new/1,webservice_helper.html\" target=\"_blank\"]WSHelper[/a] tool from jool.nl. Could that be causing problems?Thanks in advance,Tim. Quote Link to comment https://forums.phpfreaks.com/topic/9977-private-method-in-web-services-classes/ Share on other sites More sharing options...
timothee Posted May 19, 2006 Author Share Posted May 19, 2006 Never mind, David Kingma, the author of [a href=\"http://jool.nl/new/1,webservice_helper.html\" target=\"_blank\"]WSHelper[/a] pointed out my very dumb mistake: in PHP classes, I must refer to the current instance explicitely when calling methods: this->connect() instead of just connect(). Sorry for the noise.Tim. Quote Link to comment https://forums.phpfreaks.com/topic/9977-private-method-in-web-services-classes/#findComment-37083 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.