Re: Interface OOP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Feb 9, 2009 at 8:46 AM, Andrew Williams
<andrew4williams@xxxxxxxxx>wrote:

> 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;
> }
> }


just make your login class into a singleton.

class login {
  private static $instance = null;

 private function __construct() {}  // restrict access to constructor

  public static function getInstance() {
    if(is_null(self::$instance)
      self::$instance = new login();
    return self::$instance;
  }
  // ....
}

then from your other classes, you just call the login::getInstance() method
to get a handle to the 'global' or single instance of the login class.  from
there you can invoke public instance methods like GetLoginSession().

class a {
  function ExtractRawData() {
    if(login::getInstance()->GetLoginSession()) {
      // process
    }
  }

class b {
  function JohnXchange() {
    if(login::getInstance()->getLoginSession()) {
      // process
    }
  }
}

you can read about the singleton pattern in many places on the web and there
are lots of written books about design patterns which you might find useful.

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux