[PHP] PHP class question
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. -- 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP - class question
Hello, To begin.. i'm new to the list... I'm trying to learn good practices in OOP based PHP. I'm actually a bit new to OOP in general, but I've done quite a bit of reading and playing around with it in the past couple of months. What I'm trying to figure out is the proper way to call a class from a class. I dont mean by extending the class, but actaully just getting the functions... For instance... I'm trying to connect to a Database (database class) from my gen_XML class. Do I include the database class in the gen_XML constructor? Do I set a gen_XML variable as a database object (i.e. $this-mydb)? I hope my question makes some sense... Your help is appreciated... Mark Armendariz -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] PHP - class question
I would suggest you write a method (called mydb() for example) that connects to your database, and if needed, call it from your constructor like: $this-mydb(); This way you can always reuse the method from other code. hth, Steve. -Original Message- From: Mark Armendariz [mailto:[EMAIL PROTECTED]] Sent: Wednesday, August 14, 2002 2:44 PM To: [EMAIL PROTECTED] Subject: [PHP] PHP - class question Hello, To begin.. i'm new to the list... I'm trying to learn good practices in OOP based PHP. I'm actually a bit new to OOP in general, but I've done quite a bit of reading and playing around with it in the past couple of months. What I'm trying to figure out is the proper way to call a class from a class. I dont mean by extending the class, but actaully just getting the functions... For instance... I'm trying to connect to a Database (database class) from my gen_XML class. Do I include the database class in the gen_XML constructor? Do I set a gen_XML variable as a database object (i.e. $this-mydb)? I hope my question makes some sense... Your help is appreciated... Mark Armendariz -- 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] PHP - class question
If you are just trying to access a classes functions without creating an instance of it you're probably looking to use the scope resolution operator ::. For this to work all you have to do is ensure that the definition for the class you wish to use is included into the file containing the class you are trying to call the functions from. You would then call the functions as follows: ClassName::FunctionName(); At 02:43 PM 8/14/2002 -0400, you wrote: Hello, To begin.. i'm new to the list... I'm trying to learn good practices in OOP based PHP. I'm actually a bit new to OOP in general, but I've done quite a bit of reading and playing around with it in the past couple of months. What I'm trying to figure out is the proper way to call a class from a class. I dont mean by extending the class, but actaully just getting the functions... For instance... I'm trying to connect to a Database (database class) from my gen_XML class. Do I include the database class in the gen_XML constructor? Do I set a gen_XML variable as a database object (i.e. $this-mydb)? I hope my question makes some sense... Your help is appreciated... Mark Armendariz -- 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] PHP - class question
Well, I actually have a full db class which has a connect and close method as well as query, fetch_array, etc... What I really want to know is how to use the methods in my db class from another class (myclass for example).. Do I include the db class from the myclass constructor and then set a myclass variable = to the db object? ie. class my_class { var $db; function my_class() { include('class_database.php'); $this-db = new database; } ... } or is there a differnet or better way? Thanks Mark Steve Bradwell [EMAIL PROTECTED] wrote in message 57A1618E7109D311A97D0008C7EBB3A10119EF03@KITCHENER">news:57A1618E7109D311A97D0008C7EBB3A10119EF03@KITCHENER... I would suggest you write a method (called mydb() for example) that connects to your database, and if needed, call it from your constructor like: $this-mydb(); This way you can always reuse the method from other code. hth, Steve. -Original Message- From: Mark Armendariz [mailto:[EMAIL PROTECTED]] Sent: Wednesday, August 14, 2002 2:44 PM To: [EMAIL PROTECTED] Subject: [PHP] PHP - class question Hello, To begin.. i'm new to the list... I'm trying to learn good practices in OOP based PHP. I'm actually a bit new to OOP in general, but I've done quite a bit of reading and playing around with it in the past couple of months. What I'm trying to figure out is the proper way to call a class from a class. I dont mean by extending the class, but actaully just getting the functions... For instance... I'm trying to connect to a Database (database class) from my gen_XML class. Do I include the database class in the gen_XML constructor? Do I set a gen_XML variable as a database object (i.e. $this-mydb)? I hope my question makes some sense... Your help is appreciated... Mark Armendariz -- 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] PHP - class question
Do you know where I can find more info on how to use the scope resolution operator? My searches aren't giving much (at leant not mcuh regarding php) and in the manual, it only shows up momentarily as a note in the operators section... Mark Nick Oostveen [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... If you are just trying to access a classes functions without creating an instance of it you're probably looking to use the scope resolution operator ::. For this to work all you have to do is ensure that the definition for the class you wish to use is included into the file containing the class you are trying to call the functions from. You would then call the functions as follows: ClassName::FunctionName(); At 02:43 PM 8/14/2002 -0400, you wrote: Hello, To begin.. i'm new to the list... I'm trying to learn good practices in OOP based PHP. I'm actually a bit new to OOP in general, but I've done quite a bit of reading and playing around with it in the past couple of months. What I'm trying to figure out is the proper way to call a class from a class. I dont mean by extending the class, but actaully just getting the functions... For instance... I'm trying to connect to a Database (database class) from my gen_XML class. Do I include the database class in the gen_XML constructor? Do I set a gen_XML variable as a database object (i.e. $this-mydb)? I hope my question makes some sense... Your help is appreciated... Mark Armendariz -- 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[2]: [PHP] PHP - class question
Hi, Thursday, August 15, 2002, 5:17:00 AM, you wrote: MA Well, I actually have a full db class which has a connect and close method MA as well as query, fetch_array, etc... What I really want to know is how to MA use the methods in my db class from another class (myclass for example).. MA Do I include the db class from the myclass constructor and then set a MA myclass variable = to the db object? MA ie. MA class my_class { MA var $db; MA function my_class() { MA include('class_database.php'); MA $this-db = new database; MA } MA ... MA } MA or is there a differnet or better way? I set up interclass communication by having a global array for references like this: ? class a { var $t = 'Empty'; //constructor function a(){ global $classes; $classes['a'] = $this; } function test(){ echo $this-t; } } class b { var $a; //class a holder //constructor function b(){ global $classes; //if the class already exists use it... if(is_object($classes['a'])){ $this-a = $classes['a']; } //otherwise create it... else{ $this-a = new a(); } $this-a-test(); } } $a1 = new a(); $a1-t = 'This is a test'; $b1 = new b(); ? whether it is better depends on usage, but it should be good for a db class so you only make the one connection :) -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php