OO with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
victorsk
Forum Commoner
Posts: 43
Joined: Thu Apr 19, 2007 6:55 pm

OO with PHP

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
victorsk
Forum Commoner
Posts: 43
Joined: Thu Apr 19, 2007 6:55 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
  }
}
victorsk
Forum Commoner
Posts: 43
Joined: Thu Apr 19, 2007 6:55 pm

Post by victorsk »

Beautiful! Thank you so much. I think it's working now :-)


Thank you,
Victor.
Post Reply