User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 27 Oct 2002, 10:21   #1
Dudels
Guest
 
Posts: n/a
Calling An Instantiated Object's Methods From Another Method [php]

Is that possible?


Would I have the instantiated object as an argument to to method I need it in, or is there a way of accessing it without that?


Ta.
  Reply With Quote
Unread 27 Oct 2002, 11:28   #2
Dudels
Guest
 
Posts: n/a
I could make the second object inherit the properties of the first, but surely there is a better way of doing this?
  Reply With Quote
Unread 28 Oct 2002, 10:10   #3
Miserableman
Not a Cow
 
Join Date: Nov 2000
Location: Brighton, UK
Posts: 201
Miserableman is an unknown quantity at this point
I don't know php too well, but do you mean:

a) calling one method in class X from another method in class X, or

b) calling a method in class X from a method in class Y?


(a) is entirely possible, (b) is possible if the method in X is public and Y can see that particular instance of X.

Or have I got the wrong end of the stick?
__________________
<GSVsleep> cows would live a lot longer if they werent made out of steaks and leather
Miserableman is offline   Reply With Quote
Unread 28 Oct 2002, 10:37   #4
Dudels
Guest
 
Posts: n/a
The second one.


I've just gone with inheritance, turned out to be more useful for me than I had originally thought.
  Reply With Quote
Unread 28 Oct 2002, 12:50   #5
Miserableman
Not a Cow
 
Join Date: Nov 2000
Location: Brighton, UK
Posts: 201
Miserableman is an unknown quantity at this point
Would it not be something along the lines of:

Cat whiskers = new Cat();
whiskers.meow();

I am calling the meow function in the whiskers object of class Cat. Where I call it from is not important, so long as I am allowed to create a Cat.
__________________
<GSVsleep> cows would live a lot longer if they werent made out of steaks and leather
Miserableman is offline   Reply With Quote
Unread 28 Oct 2002, 19:11   #6
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Quote:
Originally posted by Miserableman
Would it not be something along the lines of:

Cat whiskers = new Cat();
whiskers.meow();

I am calling the meow function in the whiskers object of class Cat. Where I call it from is not important, so long as I am allowed to create a Cat.
I have a cat!
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 28 Oct 2002, 19:20   #7
grkn
Shadow of the past
 
Join Date: Apr 2001
Posts: 39
grkn is an unknown quantity at this point
You can hand over the pointer to the instantiated class to the method of another class.

Example:

(May be wrong, haven't bothered with php and classes for some time now)

// Class Definitions

class class_a {

function method_a($var_a){
[...]
}
}

class class_b {

function method_b($class_pointer_a, $var_c){
$blahblah = $class_pointer_a->method_a($var_c);
[...]
return;
}
}


// Instantiating classes

$class_pointer_a = new class_a();
$class_pointer_b = new class_b();


// Usage

$class_pointer_b->method_b($class_pointer_a, $var_c);
__________________
karrde, Retired head of "Myrkrs roid liberation and smuggling Inc."

One OS to rule them all, One Passport to find them,
One OS to bring them all, And with the EULA bind them.
In the land of Redmond where the windows lie.


"...to leave the Elysium the dead had to drink from and travel beyond the River of Oblivion, Lethe, to once again return to the mortal realm..."
grkn is offline   Reply With Quote
Unread 28 Oct 2002, 22:15   #8
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
why wouldnt you just call $class_pointer_a->method_a($var_c)
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 29 Oct 2002, 20:42   #9
laney
Queen of England
 
Join Date: Apr 2000
Location: not where you think
Posts: 40
laney has a spectacular aura aboutlaney has a spectacular aura aboutlaney has a spectacular aura about
Code:
class A {
  function hello() {
    echo 'hello';
  }
}

a::hello();
or am i way off the mark?!
__________________
hello
laney is offline   Reply With Quote
Unread 29 Oct 2002, 21:10   #10
dave20
bleh
 
Join Date: Mar 2001
Posts: 11
dave20 is an unknown quantity at this point
my interpretation of your question is something like this:

Code:
class classa {
 function somemethod_a()
 {
  echo "blah";
 }
}

class classb {
 function somemethod_b()
 {
  classa::somemethod_a();
 }
}
however, this won't work at all if (a) in somemethod_a(), you have any references to class variables or use $this at all.

have a look at http://www.php.net/manual/en/keyword...ekudotayim.php for some more information.
dave20 is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 15:13.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018