Page 1 of 1

OO with PHP

Posted: Tue May 22, 2007 5:17 pm
by victorsk
Hello,

I am quite new to PHP but not to OO. Problem is that I am trying to create a class whose member variables depend on another class in which case I need to use require_once() in the class body. But the problem is that it is not working. Could somebody please tell me how I can use require_once() inside a class body? Here is my code:

here is a basic idea:

Code: Select all

<?php
                    class client
                    {
                               require_once('SOAP/Client.php'); //how can I make this one work?
                               var $conn = new SOAP_Wsdl("http://ws.wsdlservice.wsdl");
                               public $client ;
                               function __construct()
                               {
                                       $client = $conn->getProxy();
                               }

                               function getClient()
                                {
                                           return $client;
                                } 

                   }
?>
So then a file.php declares:

$client = new client();

$client = $client->getClient();

Please, tell me what I am doing wrong here, the process stops when I try to instantiate $conn variable using "new SOAP_WSDL()" but it must use this package.

Please help,
Thank you,
Victor.

Posted: Tue May 22, 2007 5:25 pm
by volka

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);
require_once 'SOAP/Client.php';

class client
{
  var $conn = new SOAP_Wsdl("http://ws.wsdlservice.wsdl");
  public $client ;
  
  function __construct()
  {
    $this->client = $conn->getProxy();
  }

  function getClient()
  {
    return $this->client;
  }
}
$client and $this->client are two different variables in two different scopes.
You should set error_reporting=E_ALL in the php.ini of your development server and maybe display_errors=On and display_startup_errors=On.

Posted: Tue May 22, 2007 5:42 pm
by victorsk
Hi,

Thanks for replying. I'm getting:

Parse error: syntax error, unexpected T_NEW in /var/www/html/test/client.php on line 9

where the var $conn = new SOAP_Wsdl('http://cws.globaltsg.com/TSGWebServices ... .asmx?WSDL');

line is.

Any ideas why?

Thank you,
Victor.

Posted: Tue May 22, 2007 5:52 pm
by volka
http://de2.php.net/manual/en/language.oop5.basic.php wrote:he default value must be a constant expression, not (for example) a variable, a class member or a function call.

Code: Select all

class client
{
  // protected $conn = 1; <- would be possible.
  protected $conn;
  public $client ;
 
  function __construct()
  {
    $this->conn = new SOAP_Wsdl("http://ws.wsdlservice.wsdl");
    $this->client = $this->conn->getProxy();
  }

  function getClient()
  {
    return $this->client;
  }
}

Posted: Tue May 22, 2007 6:15 pm
by victorsk
Beautiful! Thank you so much. I think it's working now :-)


Thank you,
Victor.