each class should focus on a single area of responsibility.
therefore the login check doesn't belong in class A or B,
instead it belongs in the code consuming the functionality
of said classes.
<?php
$login = new login($pwd, $acc, $customerAcc);
$bee = new b();
if ($login->validate())
$bee->ExtractRawData();
?>
you could alternatively look into either extending the login
class or using a decorator pattern (is that the right name?),
or even registry pattern, e.g.:
<?
class A {
function extract() {
if (MyRegistry::get('login')->validate())
$this->processExtract();
}
private function processExtract() {]
}
class MyRegistry
{
private $objects = array();
function get($cls) {
if (!isset(self::$objects[$cls]) || !self::$objects[$cls]
instanceof $cls)
throw new Exception("$cls not registered!");
}
function set($obj) {
if (!is_object($obj))
throw new Exception("gimme an object!");
self::$objects[ get_class($obj) ] = $obj;
}
}
MyRegistry::set(new login($pwd, $acc, $customerAcc));
$a = new A;
$a->extract();
?>
Andrew Williams schreef:
> Hi,
>
> I am working on a back end OOP project with about about 18 classes of Object
> but some of the Objects shear one or two functions in common, like external
> login system . What is the best way to avoid a repeatable function among the
> classes without using global object instance.
>
> class a{
>
> function ExtractRawData()
> {
> global loginObj;
> *if($loginObj;->GetLoginSession(){*
> ///process
> }
> }
>
> }
>
> class b{
>
> function JohnXchange()
> {
> global loginObj;
> *if($loginObj;->GetLoginSession(){*
> ///process
> }
> }
> }
>
> class login(
>
> function loginSession($pwd, $acc, $customerAcc)
> {
> $this ->validate = connect to externalServerObject($pwd, $acc,
> $customerAcc);
> }
> *GetLoginSession()*
> {
> return $this ->validate;
> }
> }
>
>
>
> Andrew Williams
> http//www.willandy.co.uk
>
>
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php