RE: [PHP] OO Question for PHP4
[snip] Hi all. As the subject suggests, I am using PHP4 and am having something going on that I don't think should be. Presume the following code class Foo { function Foo () { return Bar; } } $foo = new Foo; echo $foo; $foo comes out as an object. Does this have to be done in two line like this?: class Foo { function bar () { return Bar; } } $foo = new Foo; $bar = $foo-bar; [/snip] This is correct. $foo = new Foo; // calls $foo as the object echo $foo; // echo's an object Bar would then be a public member of $foo, hence $foo-bar. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OO Question for PHP4
Im sure you should be returning a value in your constructor at all?? Ill check the manual, but i dont think ive ever seen a constructor return anything, doesnt sound right.. Let me check. Jason Jed R. Brubaker [EMAIL PROTECTED] wrote: Hi all. As the subject suggests, I am using PHP4 and am having something going on that I don't think should be. Presume the following code class Foo { function Foo () { return Bar; } } $foo = new Foo; echo $foo; $foo comes out as an object. Does this have to be done in two line like this?: class Foo { function bar () { return Bar; } } $foo = new Foo; $bar = $foo-bar; Or is there a better design approach I should be reminded of or learn? Thanks in advance! -- 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] OO Question for PHP4
* Jason Davidson [EMAIL PROTECTED]: Im sure you should be returning a value in your constructor at all?? Ill check the manual, but i dont think ive ever seen a constructor return anything, doesnt sound right.. Let me check. From my experience, returning a value from a constructor currently does nothing, in either PHP4 or PHP5. -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OO Question for PHP4
On Wed, 2004-08-11 at 11:14, Jed R. Brubaker wrote: Hi all. As the subject suggests, I am using PHP4 and am having something going on that I don't think should be. Presume the following code class Foo { function Foo () { return Bar; } } $foo = new Foo; echo $foo; $foo comes out as an object. Does this have to be done in two line like this?: class Foo { function bar () { return Bar; } } $foo = new Foo; $bar = $foo-bar; Or is there a better design approach I should be reminded of or learn? Thanks in advance! Constructors always return a reference to the object. A return statement is disregarded. Have a look at http://www.php.net/language.oop.constructor in particular a post in the comments by steffen staehle. HTH -- Regards, David GetAnyIdeas Web Design [EMAIL PROTECTED] P. 416.452.9410 F. 416.570.4529 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OO Question for PHP4
Yup, i typo'd .. should have read, Im not sure you should be returning a value.. Jason Matthew Weier O'Phinney [EMAIL PROTECTED] wrote: * Jason Davidson [EMAIL PROTECTED]: Im sure you should be returning a value in your constructor at all?? Ill check the manual, but i dont think ive ever seen a constructor return anything, doesnt sound right.. Let me check. From my experience, returning a value from a constructor currently does nothing, in either PHP4 or PHP5. -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- 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] OO Question for PHP4
On Wed, 11 Aug 2004 09:14:08 -0600, Jed R. Brubaker [EMAIL PROTECTED] wrote: Hi all. As the subject suggests, I am using PHP4 and am having something going on that I don't think should be. Presume the following code class Foo { function Foo () { return Bar; You shouldn't be returning from a constructor. } } $foo = new Foo; echo $foo; It's generally bad prcative to echo anything other than scalar types (strings and numbers). Try using print_r() or var_dump() instead. $foo comes out as an object. Does this have to be done in two line like this?: class Foo { function bar () { return Bar; } } $foo = new Foo; $bar = $foo-bar; Yes, this has to be two lines. I'm not sure how you'd want to put this as one. In addition, you should have parenthesis on your funciton call: $bar = $foo-bar(); Or is there a better design approach I should be reminded of or learn? -- DB_DataObject_FormBuilder - The database at your fingertips http://pear.php.net/package/DB_DataObject_FormBuilder paperCrane --Justin Patrin-- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] oo question
so, would you add a function to return all if you wish? I am finally breaking my habits with php and trying the oo approach also. So, this: function Customer($customer_id = 0) doesn't always set the customer_id to 0 even when you pass one in? Eddie -Original Message- From: Chris W. Parker [mailto:[EMAIL PROTECTED] Sent: Monday, April 19, 2004 1:33 PM To: [EMAIL PROTECTED] Subject: [PHP] oo question hi. i've recently realized that the little oo code i've written is actually not very oo at all. it's more like procedural code wrapped in a class. armed with my new knowledge i'm in the process of modifying my current classes to be more oo (or what i like to this is more oo). so i'm going to ask two questions and show two different examples stripped down to their bare minimums as to not flood the list with code. 1. my question has to do with abstraction (i think that's the right word). CLASS AS IT STANDS CURRENTLY: class Customer { function get_customer_info($customer_id) { // grab data from db // return data in form of array } } USAGE: ?php $customer_id = 45; $cust = new Customer; $customer_data = $cust-get_customer_info($customer_id); echo first name: {$customer_data[0]['fname']}\n; echo last name: {$customer_data[0]['lname']}\n; echo age: {$customer_data[0]['age']}\n; ? PROPOSED CHANGE: class Customer { var $fname; var $lname; var $age; function Customer($customer_id = 0) { if($customer_id 0) { $this-initialize_customer($customer_id); } } function initialize_customer($customer_id) { // grab data from db $this-fname = $...[0]['fname']; $this-lname = $...[0]['lname']; $this-age = $...[0]['age']; } function first_name($fname = ) { if(empty($fname)) { return $this-fname; } else { $this-fname = $fname; } } function last_name(...) { // same as above but with last name } function age(...) { // same as above but with age } } ?php $customer_id = 45; $cust = new Customer($customer_id); echo first name: .$cust-first_name().\n; echo last name: .$cust-last_name().\n; echo age name: .$cust-age().\n; ? so although the second class has a lot more code in it, it also allows me to change what happens behinds the scenes (i.e. variable names) more easily. for example the customer will always have a first_name but i may not always want to use $fname to represent it within the class. this revised class, in my limited experience, seems to be much more oo than my current class. seeing as how this email turned out longer than i had planned i will only be asking one question at this time. in fact i can't even remember what my second question was anyway. :) any and all comments are appreciated! thanks, 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] oo question
Edward Peloke mailto:[EMAIL PROTECTED] on Monday, April 19, 2004 10:49 AM said: this: function Customer($customer_id = 0) doesn't always set the customer_id to 0 even when you pass one in? no. that is the default value for a function if *no* value is passed in. chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] oo question
Chris W. Parker on Monday, April 19, 2004 11:03 AM said: Edward Peloke mailto:[EMAIL PROTECTED] on Monday, April 19, 2004 10:49 AM said: this: function Customer($customer_id = 0) doesn't always set the customer_id to 0 even when you pass one in? no. that is the default value for a function if *no* value is passed in. wait, i should have said yes, it does not always set the customer_id to 0 even when you pass one in. c. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] oo question
Chris W. Parker wrote: hi. i've recently realized that the little oo code i've written is actually not very oo at all. it's more like procedural code wrapped in a class. armed with my new knowledge i'm in the process of modifying my current classes to be more oo (or what i like to this is more oo). so i'm going to ask two questions and show two different examples stripped down to their bare minimums as to not flood the list with code. 1. my question has to do with abstraction (i think that's the right word). Really, the best of OO programing is the code abstraction, and code re-use. You should make your classes that way, as you second example does. With that on mind, you can change everything on yor classes without cnaging a single line of the code uses them. Just, you have to maintain the plublic methods used on your code, but this public functions have not to work the same way from one release to the next ... Usualy try to do that when developing a class, and you will have nice results in the future. Some of my classes have almost nothing about the original class, but method names ... and that classes continue working with old code that uses the first release of the class. If sometime in the future a method gets obsolete, what I do is maintain it for a while as a dummy method (which does nothing). In my opinion, your classes will contain much code than a procedural design, but abstraction and reuse are much more usefull than short code. Best regards, Jordi. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] oo question
Chris, Apr 19 at 10:33am, Chris W. Parker wrote: my question has to do with abstraction (i think that's the word). I think what you're talking about here is encapsulation. And yes, you are correct to point out that the second approach allows the more encapsulation of the object's data. Using methods to set or get the data stored in the object provides an interface to the object, whereas accessing the object's properties directly isn't a true interface. PROPOSED CHANGE: class Customer { ... function initialize_customer($customer_id) { // grab data from db $this-fname = $...[0]['fname']; $this-lname = $...[0]['lname']; $this-age = $...[0]['age'];} This is perfectly fine, but if you want to use those get/set methods as an interface, you're better off not setting the properties as above... How about something like this: (also took some liberties with the variable/method names...) class Customer { var $res = array(); var $id, $fname, $lname; function Customer($id=null) { $id === null || $this-initCustomer($id); } function firstName($fname=null) { $fname === null || $this-res['fname'] = $fname; return $this-res['fname']; } function lastName($lname=null) { $lname === null || $this-res['lname'] = $lname; return $this-res['lname']; } ... function initCustomer($id=null) { $id === null || $this-id = $id; $row = $db-getOne($query); // grab assoc array of db row... return $this-res = $row; } function exportCustomer() { return $this-res; } I think that may better illustrate the benefit of the interface. You can store the data inside the object, but thanks to encapsulation only the object itself needs to know how to access or manipulate the data that is stored inside. As a user of the object, you never need to know what the internals of the object are. As the developer, you don't need to worry if you want to change the structure, as long as the interface remains intact. Since you really don't want to go TOO far with the OO in PHP4, you can always store the simple scalar data in the object's properties, and just use that as a defacto interface.. for instance $obj-id = 35; is not much different from $obj-id(35), and $x = $obj-id; is similar to $obj-id(); Decide which you prefer, but encapsulation is an important OO concept. My point: it's redundant to have $obj-fname and $obj-firstName(), and may encourage mixing the way you access the object data from your code, which could potentially defeat the purpose of the get/set method. On that note, you may be interested in the __call(), __get() and __set() magic methods in PHP5, as they will give you the best of both worlds. ...Among many other nice OO feature improvements in PHP5... -- Kelly Hallman // Ultrafancy -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] oo question
Kelly Hallman mailto:[EMAIL PROTECTED] on Monday, April 19, 2004 12:34 PM said: I think what you're talking about here is encapsulation. in that case what is abstraction? PROPOSED CHANGE: class Customer { ... function initialize_customer($customer_id) { // grab data from db $this-fname = $...[0]['fname']; $this-lname = $...[0]['lname']; $this-age = $...[0]['age'];} i see my code has been run through the kellytron 5000. ;) class Customer { var $res = array(); what does $res stand for? result? var $id, $fname, $lname; function Customer($id=null) { $id === null || $this-initCustomer($id); } what is the above line equivalent to and why is '$id == null' not sufficient? My point: it's redundant to have $obj-fname and $obj-firstName(), and may encourage mixing the way you access the object data from your code, which could potentially defeat the purpose of the get/set method. i see what you mean. thanks, chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] oo question
Apr 19 at 3:46pm, Chris W. Parker wrote: Kelly Hallman mailto:[EMAIL PROTECTED] I think what you're talking about here is encapsulation. in that case what is abstraction? I suppose it's pretty similar, but I believe that encapsulation is the pedantic term for hiding the data structure behind an object's interface. I generally would consider something abstraction when it enables you to access any number of things from a common interface, such as a database abstraction layer is an API to interfacing to different RDBMS. PROPOSED CHANGE: class Customer { ... function initialize_customer($customer_id) { // grab data from db $this-fname = $...[0]['fname']; $this-lname = $...[0]['lname']; $this-age = $...[0]['age'];} i see my code has been run through the kellytron 5000. ;) Dude, it just kills me to not compact the code, at least in a list post ;) Python! Python did it to me! class Customer { var $res = array(); what does $res stand for? result? Yeah. Generally, I like compact! :) However, I am consistent throughout my code, so it makes sense if you spend enough time there! :) var $id, $fname, $lname; function Customer($id=null) { $id === null || $this-initCustomer($id); } what is the above line equivalent to Equivalent to: if ($id !== null) { $this-initCustomer($id); } The conditional: if ($id === null) { $this-initCustomer($id); } could also be written as: $id === null $this-initCustomer($id); I just find that more succinct. and why is '$id == null' not sufficient? Because where $id = 0; ($id == null) == true; due to casting, yet ($id === null) == false; === is explicit equality, the operands must also be the same type. also note that !== vs. != is like === vs. == for example: null == 0 == false == array() == '' where === would cause all to evaluate false Doesn't mean it'd be insufficient, but it would allow you to pass a zero to the method, and differentiate between it and the default value of null. -- Kelly Hallman -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OO Question
On Monday, June 10, 2002, at 12:17 PM, Jay wrote: I was wondering can I create a new Object inside of a different class? class One { //constructor function One { $this-two = new Two; $this-test = $this-two-test(); } } Can you do that? If so is that how you do it? Exactly. Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OO Question
Yes, you can do that and yes that's how you do it :) Danny. - Original Message - From: Jay [EMAIL PROTECTED] To: Php-General (E-mail) [EMAIL PROTECTED] Sent: Monday, June 10, 2002 5:17 PM Subject: [PHP] OO Question I was wondering can I create a new Object inside of a different class? class One { //constructor function One { $this-two = new Two; $this-test = $this-two-test(); } } Can you do that? If so is that how you do it? Thanks. -- 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] OO Question
Im no OO coder but I did try this before. I made (well started to anyway) a class that uses two other classes, and yes, $this-that-do_something() did work for me Jay wrote: I was wondering can I create a new Object inside of a different class? class One { //constructor function One { $this-two = new Two; $this-test = $this-two-test(); } } Can you do that? If so is that how you do it? Thanks. -- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] OO question
You can use the special name parent. e.g.: parent::baseClassFunction(); -Original Message- From: christian calloway [mailto:[EMAIL PROTECTED]] Sent: Saturday, December 08, 2001 2:16 PM To: [EMAIL PROTECTED] Subject: [PHP] OO question I want to be able to override a function/method in a parent class, but then call that function/method from within the overriden function in the child class. The Zend documentation states you can do this: In PHP 4.0 you can call member functions of other classes from within member functions or from within the global scope. You can now, for instance, override a parent function with a child function, and then call the parent function from it. But how do you do it? From the following syntax: class childclass extends parentclass .. function overridenFunction() { // call parent function wich this function overrides. $this-overridenFunction() ... } Would the method being calling the parent method or would it be calling itself? -- 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] -- 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]