[PHP] Re: PHP class question

2009-05-21 Thread Nathan Rixham

Peter van der Does wrote:

I have the following situation.

I wrote some software and split it up into functionality:

class core {
  function go{
  }
}

class A extends core {
  // PHP4 constructor
  function A {
$this-go();
  }

}

class B extends core {
}

In core I define functions and variables that are to be used
through out my program and to address those functions/variables I just
use $this- .

Now I ran into a situation where class A needs to be extended with
another class. This is not my choice, it's part of a framework I have
to use.

Currently I solved this by doing this:

class A extends framework_class {
  $var core;

  // PHP4 constructor
  function A {
$this-core = new core();
$this-core-go();
  }
}

The question I have, is this a good solution, is it the only solution
or are there different ways to tackle this?
As you might see it needs to run in PHP4.



has to extend? if it *has* to extend then you have no choice, but you 
may want to look up on class inheritance, specifically inheritance vs 
composition.


also the isa / hasa rule /should/ always apply.

which one of the following is true
1: A isa framework_class_name
2: A hasa framework_class_name

if it's 1 then you extend
if it's 2 then A should contain an instance of framework_class

eg:
class A
{
  var $framework_class = new framework_class_name();
}

you can also always proxy the methods you need.

all in, this appears to be a design pattern issue and really can't help 
any more unless you give some specifics. (like the source of your 
classes and the framework class you need to extend)


regards,

nathan

incidentally, I play inheritance vs composition as game with my 4 year 
old son, and he's really good - the untainted mind of a child can easily 
solve things us older types find more complex.

eg: car isa wheel, car hasa wheel

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: PHP class question

2009-05-21 Thread Shawn McKenzie
Peter van der Does wrote:
 I have the following situation.
 
 I wrote some software and split it up into functionality:
 
 class core {
   function go{
   }
 }
 
 class A extends core {
   // PHP4 constructor
   function A {
 $this-go();
   }
 
 }
 
 class B extends core {
 }
 
 In core I define functions and variables that are to be used
 through out my program and to address those functions/variables I just
 use $this- .
 
 Now I ran into a situation where class A needs to be extended with
 another class. This is not my choice, it's part of a framework I have
 to use.

This doesn't make sense.  You say class A needs to be extended with
another class, however what you show below is class A extending
framework_class.

Show your classes and we can help I think.

 Currently I solved this by doing this:
 
 class A extends framework_class {
   $var core;
 
   // PHP4 constructor
   function A {
 $this-core = new core();
 $this-core-go();
   }
 }
 
 The question I have, is this a good solution, is it the only solution
 or are there different ways to tackle this?
 As you might see it needs to run in PHP4.
 

I'm sure there are.  This doesn't look right to me, but I'm confused by
your examples.

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: PHP class question

2009-05-21 Thread Peter van der Does
On Thu, 21 May 2009 14:08:11 -0500
Shawn McKenzie nos...@mckenzies.net wrote:


 
 This doesn't make sense.  You say class A needs to be extended with
 another class, however what you show below is class A extending
 framework_class.
 

I worded it wrong, I apologize.
Class A needs to be an extension of the framework class.

-- 
Peter van der Does

GPG key: E77E8E98
IRC: Ganseki on irc.freenode.net
Blog: http://blog.avirtualhome.com
Forums: http://forums.avirtualhome.com
Jabber ID: pvanderd...@gmail.com

GetDeb Package Builder
http://www.getdeb.net - Software you want for Ubuntu

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: PHP class question

2009-05-21 Thread Shawn McKenzie
Peter van der Does wrote:
 On Thu, 21 May 2009 14:08:11 -0500
 Shawn McKenzie nos...@mckenzies.net wrote:
 
 
 This doesn't make sense.  You say class A needs to be extended with
 another class, however what you show below is class A extending
 framework_class.

 
 I worded it wrong, I apologize.
 Class A needs to be an extension of the framework class.
 

Well I guess from my point of view there are several ways depending upon
the requirements.  Others that are better with OOP will chime in I'm sure.

This I'll get flamed for, but you can use one instance of core as a global:

class A extends framework_class {
  var $core;

  function A() {
$this-core = $GLOBALS['core'];
$this-core-go();
  }
}

//in global scope in bootstrap or whatever
$core = new core();

Along the same lines but more OOP and without globals, maybe use a
registry class and store core in the registry. This also uses one
instance of core:

class Registry {
protected $_objects = array();

function set($name, $object) {
$this-_objects[$name] = $object;
}

function get($name) {
return $this-_objects[$name];
}
}

class A extends framework_class {
  var $core;

  function A($registry) {  //dunno if you need a reference here or not
$this-core = $registry-get('core');
$this-core-go();
  }
  //i guess you could also pass in core, but registry will give you all
objects in the registry
  //function A(core) {
//$this-core = $core;
//$this-core-go();
  //}
}

//this is in your bootstrap or whatever
$core = new core();
$registry = new registry();
$registry-set('core', $core);

Or, if you don't need an object, call it statically:

class A extends framework_class {

  function A() {
core::go();
  }
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: PHP class question

2009-05-21 Thread Shawn McKenzie
Shawn McKenzie wrote:
 Peter van der Does wrote:
 On Thu, 21 May 2009 14:08:11 -0500
 Shawn McKenzie nos...@mckenzies.net wrote:


 This doesn't make sense.  You say class A needs to be extended with
 another class, however what you show below is class A extending
 framework_class.

 I worded it wrong, I apologize.
 Class A needs to be an extension of the framework class.

 
 Well I guess from my point of view there are several ways depending upon
 the requirements.  Others that are better with OOP will chime in I'm sure.
 
 This I'll get flamed for, but you can use one instance of core as a global:
 
 class A extends framework_class {
   var $core;
 
   function A() {
 $this-core = $GLOBALS['core'];
 $this-core-go();
   }
 }
 
 //in global scope in bootstrap or whatever
 $core = new core();
 
 Along the same lines but more OOP and without globals, maybe use a
 registry class and store core in the registry. This also uses one
 instance of core:
 
 class Registry {
 protected $_objects = array();
 
 function set($name, $object) {
 $this-_objects[$name] = $object;
 }
 
 function get($name) {
 return $this-_objects[$name];
 }
 }
 
 class A extends framework_class {
   var $core;
 
   function A($registry) {  //dunno if you need a reference here or not
 $this-core = $registry-get('core');
 $this-core-go();
   }
   //i guess you could also pass in core, but registry will give you all
 objects in the registry
   //function A(core) {
 //$this-core = $core;
 //$this-core-go();
   //}
 }
 
 //this is in your bootstrap or whatever
 $core = new core();
 $registry = new registry();
 $registry-set('core', $core);
 
 Or, if you don't need an object, call it statically:
 
 class A extends framework_class {
 
   function A() {
 core::go();
   }
 }
 

I guess you could always do a singleton so that you always have the same
instance.  Add something like this to the core class (not tested):

static $_instance;

function getInstance() {
if(self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}

Then you can do:

class A extends framework_class {
   var $core;

   function A() {
 $this-core = core::getInstance();
 $this-core-go();
   }
 }

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: PHP class question

2009-05-21 Thread Nathan Rixham

Shawn McKenzie wrote:

Shawn McKenzie wrote:

Peter van der Does wrote:

On Thu, 21 May 2009 14:08:11 -0500
Shawn McKenzie nos...@mckenzies.net wrote:



This doesn't make sense.  You say class A needs to be extended with
another class, however what you show below is class A extending
framework_class.


I worded it wrong, I apologize.
Class A needs to be an extension of the framework class.


Well I guess from my point of view there are several ways depending upon
the requirements.  Others that are better with OOP will chime in I'm sure.

This I'll get flamed for, but you can use one instance of core as a global:

class A extends framework_class {
  var $core;

  function A() {
$this-core = $GLOBALS['core'];
$this-core-go();
  }
}

//in global scope in bootstrap or whatever
$core = new core();

Along the same lines but more OOP and without globals, maybe use a
registry class and store core in the registry. This also uses one
instance of core:

class Registry {
protected $_objects = array();

function set($name, $object) {
$this-_objects[$name] = $object;
}

function get($name) {
return $this-_objects[$name];
}
}

class A extends framework_class {
  var $core;

  function A($registry) {  //dunno if you need a reference here or not
$this-core = $registry-get('core');
$this-core-go();
  }
  //i guess you could also pass in core, but registry will give you all
objects in the registry
  //function A(core) {
//$this-core = $core;
//$this-core-go();
  //}
}

//this is in your bootstrap or whatever
$core = new core();
$registry = new registry();
$registry-set('core', $core);

Or, if you don't need an object, call it statically:

class A extends framework_class {

  function A() {
core::go();
  }
}



I guess you could always do a singleton so that you always have the same
instance.  Add something like this to the core class (not tested):

static $_instance;

function getInstance() {
if(self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}

Then you can do:

class A extends framework_class {
   var $core;

   function A() {
 $this-core = core::getInstance();
 $this-core-go();
   }
 }



bullseye, static is the way to approach when you only need a single instance

however you don't always need to include a referent to the instance 
inside your class.. you can easily


core::getInstance()-go()
or
core::go()

and have class core store an instance of itself

class core {
  var $instance;
  function getInstance()

..etc - been so long since i touched php4

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php