Re: [PHP] Re: PHP class question
Shawn McKenzie wrote: Shawn McKenzie wrote: Peter van der Does wrote: On Thu, 21 May 2009 14:08:11 -0500 Shawn McKenzie 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
Re: [PHP] Re: PHP class question
Shawn McKenzie wrote: > Peter van der Does wrote: >> On Thu, 21 May 2009 14:08:11 -0500 >> Shawn McKenzie 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
Peter van der Does wrote: > On Thu, 21 May 2009 14:08:11 -0500 > Shawn McKenzie 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
On Thu, 21 May 2009 14:08:11 -0500 Shawn McKenzie 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
[PHP] Re: PHP class question
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
[PHP] Re: PHP class question
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
Re: [PHP] Class question
What does "$this->A(); "" do ?(in constructor of class B) does it make another instance of class A, and if not why do I have to parse vars to the A constructor ($this->A($var1,$var2))?. I dont think that is what I want. A can have many instances of B. B can have many instances of C. A B B B B Should not a instanse(object) of class B bee able to request av var of the calling object (class A)? Sorry about this stupid questions, I'm not a coder. Thomas Munz wrote: Use 'extends' syntax for sub classes class A { var $some_var; var $class_B; function A(){ $this->class_B = new B(); $this->some_var=2; } } class B extends A { var $class_C; function B(){ $this->A(); $this->class_C = new C(); // $this->some_var= some_var_from_class_A; <-- $this->some_var contains the value allready } } class C extends A { function C(){ $this->A(); // $this->some_var= some_var_from_class_A; <-- $this->some_var contains the value allready } } on Friday 15 September 2006 11:01, Roger Helgesen wrote: I have 3 classes, CLASS A,B and C class A { var $some_var; var $class_B; function A(){ $this->class_B = new B(); $this->some_var=2; } } class B { var $some_var; var $class_C; function B(){ $this->class_C = new C(); $this->some_var= some_var_from_class_A; } } class C { var $some_var; function C(){ $this->some_var= some_var_from_class_A; } } How can class B and C get the value of $some_var_class_A without using "$this->class_B = new B($some_var);" roger helgesen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class question
Well in this example it will get to an infinite loop, but anyway like Thomas said you must check into extending classes. Andy Thomas Munz wrote: > Use 'extends' syntax for sub classes > > class A { > var $some_var; >var $class_B; > function A(){ > $this->class_B = new B(); > $this->some_var=2; > } > } > > class B extends A { > var $class_C; > function B(){ > $this->A(); > $this->class_C = new C(); >// $this->some_var= some_var_from_class_A; <-- > $this->some_var contains the value allready > } > } > > class C extends A { > function C(){ > $this->A(); > // $this->some_var= some_var_from_class_A; <-- > $this->some_var contains the value allready > } > } > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class question
Use 'extends' syntax for sub classes class A { var $some_var; var $class_B; function A(){ $this->class_B = new B(); $this->some_var=2; } } class B extends A { var $class_C; function B(){ $this->A(); $this->class_C = new C(); // $this->some_var= some_var_from_class_A; <-- $this->some_var contains the value allready } } class C extends A { function C(){ $this->A(); // $this->some_var= some_var_from_class_A; <-- $this->some_var contains the value allready } } on Friday 15 September 2006 11:01, Roger Helgesen wrote: > I have 3 classes, CLASS A,B and C > > class A { > var $some_var; >var $class_B; > function A(){ > $this->class_B = new B(); > $this->some_var=2; > } > } > > class B { > var $some_var; >var $class_C; > function B(){ > $this->class_C = new C(); > $this->some_var= some_var_from_class_A; > } > } > > class C { > var $some_var; > > function C(){ > $this->some_var= some_var_from_class_A; > } > } > > > > How can class B and C get the value of $some_var_class_A without using > "$this->class_B = new B($some_var);" > > > roger helgesen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] class question 2
once their instantiated, they're just like any other variable - that is: if a "normal" variable, declared in the same place as the object, is accessable in another function, then the object will be too. HTH Martin -Original Message- From: Caspar Kennerdale [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 28, 2002 1:17 PM To: Php-General-List (E-mail) Subject: [PHP] class question 2 Sorry Another question- Do instances of classes remain in 'communication' with each other if declared my different functions in a case switch statement. At present I have an admin.php which is one long swtich statement. Wihin each is a list iof functions which created the elements of the page Each of these functions are defined in anither file and are instances of classes. If I create an instance of a DB class for exapmle- can I refer to it thoughout the visit to the website- ie does it maintaoin its values and variables even if different functions freom the main index.php page are called sorry if this is really confusing any ideas much appreciated -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] class question 2
Objects which are instantiated from classes on one page are not available on the next page. You have to use session functionality. -Original Message- From: Caspar Kennerdale [mailto:[EMAIL PROTECTED]] Sent: Wednesday, March 27, 2002 8:17 PM To: Php-General-List (E-mail) Subject: [PHP] class question 2 Sorry Another question- Do instances of classes remain in 'communication' with each other if declared my different functions in a case switch statement. At present I have an admin.php which is one long swtich statement. Wihin each is a list iof functions which created the elements of the page Each of these functions are defined in anither file and are instances of classes. If I create an instance of a DB class for exapmle- can I refer to it thoughout the visit to the website- ie does it maintaoin its values and variables even if different functions freom the main index.php page are called sorry if this is really confusing any ideas much appreciated -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class question
Well, I guess many things are "accepted" now, but here's my two cents: I wouldn't access global variables, instead you should pass everything you need as parameters. Using global variables can come back to bite you occasionally because you may think you are changing a local variable at one point, but you actually change a global variable, and then who knows what will happen when you try to access the global variable later in your code expecting an int, for example, and it is suddenly a string... As for using other user defined functions from elsewhere in your code, I think it depends on the context... But think about what if you want to use this class elsewhere, then you will always need to copy over whatever file contains your other functions. In that case it would obviously make more sense to contain whatever functions are needed inside of the class. Hope that gives you some ideas. Jeff At 02:58 PM 2/20/2002 -0600, Chris Boget wrote: >I'm still kind of new when it comes to dealing with classes. >I'm curious, is it accepted practice for member functions of >your class to: > >* access global variables >* use non member, user defined functions > >Chris > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Class question
The whole idea with object-oriented programming is polymorphism and encapsulation. You do not want your classes to access global variables but access that information through objects and messaging between objects. If you are referring to the use of non-member as being outside of the class, then again you want to use objects. As for practice, it is up to you. For me, no... Ray Hunter Firmware Engineer ENTERASYS NETWORKS -Original Message- From: Chris Boget [mailto:[EMAIL PROTECTED]] Sent: Wednesday, February 20, 2002 1:58 PM To: PHP General Subject: [PHP] Class question I'm still kind of new when it comes to dealing with classes. I'm curious, is it accepted practice for member functions of your class to: * access global variables * use non member, user defined functions Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php