Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to call class function from another class
Old 07-10-2009, 02:13 PM how to call class function from another class
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
So I'm managing two class/objects... one for the main site and one for mobile but to reduce redundancy I didn't duplicated general functions... how do I reference the function in another class..

eg:

PHP Code:

$object_one 
= new object_one;
$object_two = new object_two
in object two I want to do this

PHP Code:

class object_two {
function() {
$object_one->function_one();
}

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-10-2009, 02:48 PM Re: how to call class function from another class
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Assuming the class name is object_one you can call it by object_one::function_one()
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-10-2009, 02:55 PM Re: how to call class function from another class
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
I've actually tried that and it didn't work

i actually figured it out over time and hopefully this could help others

PHP Code:

$object_one 
= new object_one;
$object_two = new object_two($object_two); 
PHP Code:
//Object Two

class object_two {
var 
object_one;

function 
object_two(object_one) {
$this->object_one $object_one;
}

//Calling Functions
function something_cool() {
//Call
$this->object_one->another_function();
}

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by orionoreo; 07-10-2009 at 02:56 PM.. Reason: Code Fix
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 07-10-2009, 06:10 PM Re: how to call class function from another class
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Assuming the class name is object_one you can call it by object_one::function_one()
But only if the method is declared as static (I believe).
A static method being a method that is shared by every instance of a given class.

The second way is "proper" in my opinion, at one exception.
Be aware that PHP does a copy of $object_one when you pass it as a parameter to $object_two.
This means, that if you change somethinf from the inside of $object_two to the $object_one object that is encapsulated in it, it will not reflect on the real $object_one!

To avoid this, you have to use what is called a "passage by reference" of the object $object_one:
PHP Code:
//Object Two

class object_two {
  var 
object_one;

  function 
object_two(object_one) {
    
$this->object_one $object_one;
  }

  
//Calling Functions
  
function something_cool() {
    
//Call
    
$this->object_one->another_function();
  }
}  

$object_one = new object_one;
$object_two = new object_two(&$object_one); 
Notice the "&" before $object_one, this is what tells the PHP engine not to copy the object, but to share the same reference to the place where the object is stored in memory.Here is the PHP.net doc page relative to this:
http://us3.php.net/manual/en/languag...ences.pass.php
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-10-2009, 07:51 PM Re: how to call class function from another class
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
ah understood... code modified.. ty
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how to call class function from another class
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55626 seconds with 12 queries