I'm starting with OOP for php, i get the idea, but don't have someone physical near me to fallback on so I'm hoping i can do that here.
The following works but i would like to know if it's good practice, that is something that weights a lot for me.
Concept is that you can create an object, the data for this object is gotten trough different soap calls. I have (at this point) a class to create the details and a class to connect soap and get the details.
Here it is: http://pastebin.com/9bgKzATv
So again, just hoping someone is willing and has the time to give it a quick look and just shoot at it so i can learn.
I know i actually should use a dependency in the create, to the soap, but the way it's set up now does make the code cleaner and easyer...
EDIT: i see the code must be included and not a link:
###
# INDEX
###
<?php
function __autoload( $class )
{
if( !defined( 'ROOT_DIR_CLASSES' ) )
{
define( 'ROOT_DIR_CLASSES', realpath( __DIR__ . DIRECTORY_SEPARATOR . 'classes' ) );
}
$directory = str_replace( '\\', DIRECTORY_SEPARATOR , $class );
$file = ROOT_DIR_CLASSES . DIRECTORY_SEPARATOR . $directory . '.class.php';
if( file_exists( $file ) )
{
include( $file );
}
}
$objectId = 0123;
$soapUser = 'User';
$soapPassword = 'Pass';
use Vendor\Object\Details\Create as Yacht;
try
{
$yacht = new Yacht( $objectId, $soapUser, $soapPassword );
print '<pre>';
print_r( $yacht->objectSpecs() );
}
catch( Exception $e )
{
var_dump( $e );
}
?>
##
# CREATE DETAILS
##
<?php
namespace Vendor\Object\Details;
class Create
{
public $objectId;
private $_soapUser;
private $_soapPassword;
private $_soapCall;
//construct our object
public function __construct( $objectId, $_soapUser, $_soapPassword )
{
$this->objectId = (string) $objectId;
$this->_soapUser = $_soapUser;
$this->_soapPassword = $_soapPassword;
$this->_soapCall = new \Vendor\Soap\Call\SoapGet( $this->_soapUser, $this->_soapPassword );
}
//make the details array
public function objectSpecs()
{
try
{
$soapObjectDetails = $this->_soapCall->GetDetails( $this->objectId );
}
catch( Exception $e )
{
throw $e;
}
if( is_array( $soapObjectDetails ) )
{
return $soapObjectDetails;
}
throw new \Exception('Requested object has no specs');
}
}
?>
##
# SOAP CALLS
##
<?php
namespace Vendor\Soap\Call;
class SoapGet
{
private $_soapUser;
private $_soapPassword;
public $soapCall;
public $soapParams;
public $objectId;
function __construct( $_soapUser, $_soapPassword )
{
$this->_soapUser = $_soapUser;
$this->_soapPassword = $_soapPassword;
if( !isset( $this->_soapUser ) OR empty( $this->_soapUser ) )
{
throw new \Exception('No SOAP user given');
}
if( !isset( $this->_soapPassword ) OR empty( $this->_soapPassword ) )
{
throw new \Exception('No SOAP password given for user ' . $this->_soapUser);
}
$this->soapParams = array(
'user' => $this->_soapUser,
'password' => $this->_soapPassword,
);
$this->soapCall = new \SoapClient(
null,
array(
'location' => 'http://localhost/webservice/webservice.php',
'uri' => 'http://localhost/webservice.php',
'trace' => 1,
)
);
}
function getDetails( $objectId )
{
if( !isset( $this->soapParams ) OR empty( $this->soapParams ) )
{
throw new \Exception('soapParams are empty');
}
$this->soapParams['yachtId'] = $objectId;
//Soap expects a string encoded objectid
$object = $this->soapCall->__soapCall( 'getDetails', $this->soapParams );
$object = base64_decode( $object );
$object = unserialize( $object );
return $object;
}
}
?>