[PHP] PHP OOP Question
Is it bad practice for a class to have a variable and method of the same name? Well, it's a matter of opinion what is and isn't bad practice, but personally I would never give a variable and a function the same name - if only to avoid confusion (PHP shouldn't mind if you do so though). Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP OOP Question
Is it bad practice for a class to have a variable and method of the same name? For example: class product { var $Name; function Name() { return stripslashes($this-Name); } } Thanks, -Ed This message was sent using IMP, the Internet Messaging Program. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] OOP Question
What do members think that this code should do: class A { var $a, $b; function A($a) { $this-$a = $a; } function prnt() { echo bra= . $this-$a; } } class B extends A { function B($a, $b) { parent::A($a); $this-$b = $b; } function prnt() { parent::prnt(); echo brb= . $this-$b; } } $obj = new B(a,b); $obj-prnt(); I think it should print a=a b=b but it seems to print (on PHP4) a= b= Cheers, Richard PS. In C++ I would be thinking of virtual functions and in Java I would be thinking of public/protected properties. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP Question
On Wed, 2004-04-07 at 17:07, Richard Lewis wrote: What do members think that this code should do: class A { var $a, $b; function A($a) { $this-$a = $a; } function prnt() { echo bra= . $this-$a; } } class B extends A { function B($a, $b) { parent::A($a); $this-$b = $b; } function prnt() { parent::prnt(); echo brb= . $this-$b; } } $obj = new B(a,b); $obj-prnt(); I think it should print a=a b=b but it seems to print (on PHP4) a= b= Looks like you're suffering from mad dollar disease. You should have the following instead: class A { var $a, $b; function A($a) { $this-a = $a; } function prnt() { echo bra= . $this-a; } } class B extends A { function B($a, $b) { parent::A($a); $this-b = $b; } function prnt() { parent::prnt(); echo brb= . $this-b; } } $obj = new B(a,b); $obj-prnt(); -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] OOP question
Is it possible to reference two instances of an object at once with a single variable name while retaining the ability to reference the objects seperately? I am using the PEAR Template IT package and need to generate two similar but different results at the same time. I am doing this with two instances of the IT class ($tpl1 and $tpl2). I have some code like this: $tpl1-setVariable(var1,This); $tpl2-setVariable(var1,This); $tpl1-setVariable(var2,That); $tpl2-setVariable(var2,That); $tpl1-setVariable(var3,Something); $tpl2-setVariable(var3,SomethingElse); I would like for this to be: $tpl3-setVariable(var1,This); $tpl3-setVariable(var2,That); $tpl1-setVariable(var3,Something); $tpl2-setVariable(var3,SomethingElse); Where $tpl3 is a reference to both $tpl1 and $tpl2. Is this possible at all? It seems to make for very clean code for me, but I can't think of how it could be possible. Thanks in advance. -Jackson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP question
On Sat, 1 Jun 2003, Jackson Miller wrote: Is it possible to reference two instances of an object at once with a single variable name while retaining the ability to reference the objects seperately? No chance. Well, you could hack it with some really fancy object overloading, but in general, no, you cannot have a single reference that references two distinct objects. References work the other way around. Given a single distinct object, you can create multiple equivalent references to it. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP question
At 08:39 01.06.2003, Jackson Miller said: [snip] Is it possible to reference two instances of an object at once with a single variable name while retaining the ability to reference the objects seperately? [snip] Not using references. I once built a small class that handles such stuff. Basically it's a container for object references that are attached to it, and it will execute any method to all objects simultaneously. Code plus testbed is attached below. The base implementation of CTeeObject checks if that all attached objects are of the same class of the first attached object, or at least derived from it. This is how I needed it but it's not mandatory, as the call agent method or CTeeObject checks each attached object if the method exists. Upon attaching objects, CTeeObject calls its check_object() method if the object is ok to attach - you may override this method in a derived class to perform less or additional checks. For example you could provide a specialized CTeeObject-derived class that doesn't use the generic call agent (for performance reasons) but calls object methods directly (which would be quite feasible in your case). Use the overridable check_object() to check if the object to attach has a supported class. ?php class CTeeObject { var $arObj; var $class; function CTeeObject() { // we do nothing in the constructor class // use the attach() method $this-arObj = array(); } function attach($hObj) { // if this is the first object, simply accept it. // for all successive objects make sure they have // the same class (or at least derived from). if (!count($this-arObj)) { if (is_object($hObj)) { $this-arObj[] = $hObj; $this-class = get_class($hObj); } } else { if (!$this-check_object($hObj)) return false; $this-arObj[] = $hObj; } return true; } // override this method in your derived class // for additional class checks function check_object($hObj) { return is_a($hObj, $this-class); } // generic call agent function call_method($method) { // accessing unlisted arguments will trigger a notice/warning // this is unavoidable here so modify error reporting $erep = error_reporting(E_ERROR); // build a list of arguments to pass $arglist = array(); for ($i = 1; $i func_num_args(); ++$i) $arglist[] = func_get_arg($i); // build the method call $cmd = null; for ($i = 0; $i count($arglist); ++$i) $cmd .= ($cmd ? ',' : null) . \$arglist[$i]; $cmd = return \$hObj-$method($cmd);; // error reporting to standard // to have it available in the object implementation error_reporting($erep); // now walk all attached objects and execute the required method // but check if the object is still an object and that the method exists $result = array(); for ($i = 0; $i count($this-arObj); ++$i) { $hObj = $this-arObj[$i]; if (is_object($hObj) method_exists($hObj, $method)) $result[] = eval($cmd); else $result[] = null; } return $result; } } // Test Bed class CBaseTest { var $id; function CBaseTest($id) { $this-id = $id; } function hello($hellostring) { echo sprintf('Hello from %s (%s): %sbr /', get_class($this), $this-id, $hellostring); return $this-id; } } class CExtendedTest extends CBaseTest { function say($what, $second) { echo sprintf('SayWhat from %s (%s): %s - %dbr /', get_class($this), $this-id, $what, $second); return $this-id; } } $tee = new CTeeObject; $tee-attach(new CBaseTest(1)); $tee-attach(new CExtendedTest(1)); $tee-attach(new CBaseTest(2)); $tee-attach(new CExtendedTest(2)); $tee-attach(new CBaseTest(3)); $tee-attach(new CExtendedTest(3)); $tee-attach(new CBaseTest(4)); $tee-attach(new CExtendedTest(4)); echo 'pre';
Re: [PHP] OOP question
Just noticed a small glitch in the attach() method - check_object will not be called for the first object. Use this attach() / check_object() implementation instead. The var $class instance variable of CTeeObject can be omitted. function attach($hObj) { if (!$this-check_object($hObj)) return false; $this-arObj[] = $hObj; return true; } // override this method in your derived class // for additional class checks function check_object($hObj) { // if this is the first object, simply accept it. // for all successive objects make sure they have // the same class (or at least derived from). if (is_object($hObj)) return (!count($this-arObj) || is_a($hObj, get_class($this-arObj[0]))); return false; } -- O Ernest E. Vogelsinger (\)ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP Question in PHP
Scott Hurring [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Depends ;-) yea, really depnds, but as you mentioned, having parameterless constructors are more generic, especially when it comes to derive the classes. instead (with option #2), you could do something like: // Create new document $d = new Document(); // populate document $d-save(); and // Get existing doc $d = new Document(); $d-get(ID); i'd advise method names load() (as for retrieve) and save() (as for store) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] OOP Question in PHP
Hello. I have, what is probably a silly, question. I want to convert my existing scripts to OO - because it's the way to go in terms of the huge amount of functionality that they now have (and since I've never done it before it would be interesting). However I have one thing that I don't understand. My scripts manipulate documents and these documents are stored in a MySQL database. To create a new object is really easy. However, since I have lots of documents already, how do I alter one? Do I create a new document and pass the primary key as it's a variable in the $document = new Document(number); and have the constructor retrieve the appropriate data from MySQL and fill the attribute variables. Or do I do something like: $document = new Document; $document -retrieve(number); Which is the best and most valid way of doing things. Cheers, Henry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] OOP Question in PHP
Depends ;-) I'd say that the second way is probably best, becuase it avoids implied behaviour, which sometimes can cause problems (lets say you want to init. a document object, but not get anything from the DB). If you want to create a brand new Document, obviously it won't be in the DB already, so you might not want to have the constructor go and try to fetch it. instead (with option #2), you could do something like: // Create new document $d = new Document(); // populate document $d-save(); and // Get existing doc $d = new Document(); $d-get(ID); But that's just my view... there are as many different views on OO as there are programmers :) --- Scott Hurring Systems Programmer EAC Corporation [EMAIL PROTECTED] Voice: 201-462-2149 Fax: 201-288-1515 -Original Message- From: Henry Blackman [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 05, 2002 5:33 PM To: [EMAIL PROTECTED] Subject: [PHP] OOP Question in PHP Hello. I have, what is probably a silly, question. I want to convert my existing scripts to OO - because it's the way to go in terms of the huge amount of functionality that they now have (and since I've never done it before it would be interesting). However I have one thing that I don't understand. My scripts manipulate documents and these documents are stored in a MySQL database. To create a new object is really easy. However, since I have lots of documents already, how do I alter one? Do I create a new document and pass the primary key as it's a variable in the $document = new Document(number); and have the constructor retrieve the appropriate data from MySQL and fill the attribute variables. Or do I do something like: $document = new Document; $document -retrieve(number); Which is the best and most valid way of doing things. Cheers, Henry -- 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] OOP Question in PHP
On Wed, Jun 05, 2002 at 10:32:39PM +0100, Henry Blackman wrote: Do I create a new document and pass the primary key as it's a variable in the $document = new Document(number); and have the constructor retrieve the appropriate data from MySQL and fill the attribute variables. Or do I do something like: $document = new Document; $document -retrieve(number); Which is the best and most valid way of doing things. You could do both, and use whatever you find more appropriate at that moment: class Document { function Document($id='') { if($id) { $this-retrieve($id); } } function retrieve($id) { //do something } } BTW I get the feeling that you want retrieve() to output directly to the browser (echo/print...), IMHO you shouldn't do that at all but let retrieve() return a string/array/whatever. But that's up to you :) -- Daniel Tryba -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] OOP question regarding class extension
Can one class extend multiple classes? I'm still playing with OOP functionality and maybe my logic is reversed but I think I need one class to extend many others. John Guynn This email brought to you by RFCs 821 and 1225. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]