...

View Full Version : Class calling function in another class



dniwebdesign
05-09-2006, 05:10 AM
Alright... I have a function called getName() in a class called info.
Then I have another function called getDropDown() in a class called adminFunctions. Now; I include these two classes into a normal php page called "category.php" like this:


include("info.class.php");
$info = new info;
include("admin.class.php");
$theAdmin = new adminFunctions;


The question I have is how would I call the getName function in the info class from the getDropDown function in the adminFunctions class. I know when calling a function in the same class I would use $this, but I'm not sure about calling from a different class.

Aruna Attanayak
05-16-2006, 09:11 AM
//first require nessary class filse
//assume your class files are in the same directory.
require './info.class.php';
require './admin.class.php';

$info = new info;//creating object from the perticular class
$theAdmin = new adminFunctions;

//this is how you call the methods
$name=$info->getName();
$drpDown=$theAdmin->getDropDown() ;

good luck...

GJay
05-16-2006, 09:31 AM
I don't think that's quite what he's after.
One option is to pass the first object to the second, either as part of the constructor, via some kind of purpose-built setter, or to the function that's going to use it, e.g. (the constructor approach)


class AdminFunctions {
function AdminFunctions($info) {
$this->info=$info;
}

function getDropDown() {
$this->info->someFunction();
}
}
$info=new info;
$admin=new AdminFunctions($info);

The second option is to use the 'global' keyword in function where you want the other class:


class AdminFunction {
...
function getDropDown() {
global $info;
$info->someFunction();
}
}


A this approach is to use the concept of a singleton, you'll have a globally accessible function called getInfo() that (using static variables) returns an 'info' object (after creating one if it doesn't exist), but I have to go now so no time for an example (this is a better approach than using globals though)

firepages
05-16-2006, 11:51 AM
if the getName() does not require the instansiation of the class you can also make a static call ..

$blah=info::getName();

aggregation is my favourite approach ...



<?php
class AdminFunctions {
var $info;//external info object
function AdminFunctions() {
$this->info= new info();
}

function getDropDown() {
$this->info->someFunction();
}
}

$admin=new AdminFunctions();
?>

GJay
05-16-2006, 12:10 PM
You wouldn't want to pass $info to the constructor, and then instantiate a new 'info' though, surely?

From the classnames, i've got a feeling that the data-modelling isn't as good as it could be, and so choosing the best approach is difficult (if $info is some kind of widely-used utility, then the global/singleton approach is probably better, if it's just used within AdminFunction then passing it to the object is...)

firepages
05-16-2006, 04:34 PM
You wouldn't want to pass $info to the constructor, and then instantiate a new 'info' though, surely?
lol no , typo = fixed cheers :D[/QUOTE]



From the classnames, i've got a feeling that the data-modelling isn't as good as it could be, and so choosing the best approach is difficult (if $info is some kind of widely-used utility, then the global/singleton approach is probably better, if it's just used within AdminFunction then passing it to the object is...)

Deciding how to model such relationships does seem to have an inbuilt `errr` factor, I mostly only use singletons for DAO's or similar and not for anything that has to think too much... but then again I have at times regretted that as well ~ .. but I still work mostly with PHP4 so perhaps thats limiting .. then again I forsee even more complexities with abtracts and interfaces etc (even though I see the usefulness)

marek_mar
05-16-2006, 06:47 PM
How about this method?


<?php
class foo
{
function hello()
{
print 'Hello' . "\n";
}
}

class bar
{
function bye()
{
print 'Bye' . "\n";
}
}

class family
{
function __construct()
{
print 'Family' . "\n";
}
static function adopt()
{
runkit_class_adopt('family', 'foo');
runkit_class_adopt('family', 'bar');
}
}

family::adopt();
$family = new family();

$family->hello();
$family->bye();
/* Ouptuts:
Family
Hello
Bye
*/
?>

trib4lmaniac
05-17-2006, 10:36 AM
How about this method?


<?php
class foo
{
function hello()
{
print 'Hello' . "\n";
}
}

class bar
{
function bye()
{
print 'Bye' . "\n";
}
}

class family
{
function __construct()
{
print 'Family' . "\n";
}
static function adopt()
{
runkit_class_adopt('family', 'foo');
runkit_class_adopt('family', 'bar');
}
}

family::adopt();
$family = new family();

$family->hello();
$family->bye();
/* Ouptuts:
Family
Hello
Bye
*/
?>

Multiple inheritance? I think I'd stick with composition :)
Didn't know about runkit before though.

marek_mar
05-17-2006, 04:32 PM
Runkit can do some nice things... but I've yet to see a server with it enabled.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum