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
Old 07-11-2006, 10:41 AM Multiple Classes?
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
If i have 2 classes how do take a variable from one while inside another class. This is what i had but it doesn't work:

example:

file: example1.php
PHP Code:
class class1
{
var 
variable;
}
$class1 = &New class1;
$class1->variable "Hello World"
file: example2.php
PHP Code:
include ("example1.php")

class 
class2
{
    function 
get_variable()
    {
    
$variable $class1->variable;
    return (
$variable);
    }
}
$class2 = &New class2;
$variable $class2->get_variable();
echo 
$variable
timsquash5 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-11-2006, 11:15 AM Re: Multiple Classes?
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Example 1:
PHP Code:
<?php

class class1
{
  var 
$variable  "Hello World";  
}

class 
class2
{
    var 
$_obj;
    
    function 
class2($obj){
        
$this->_obj=$obj;
    }
    
    function 
get_variable()
    {  
          return (
$this->_obj->variable;);
    }
}
$class1 = new class1();
$class2 = new class2($class1);
$variable $class2->get_variable();
echo 
$variable;  
?>
Example 2:

PHP Code:
<?php

class class1
{
  var 
$variable  "Hello World";  
}

class 
class2
{
    function 
get_variable()
    {  
       
$class1 = new class1();
       
$variable $class1->variable;
       
       return (
$variable);
    }
}



$class2 = new class2();
$variable $class2->get_variable();
echo 
$variable;  

?>
You were almost with it but you failed to either create a new class 1 obj within class 2 or pass class1 when creating class2.

This is called Aggregation and is when one class has access to another, the second class perhaps having been instantiated outside the first. If the first object “dies”, the second will continue to “live”. This is commonly seen with data access objects, which may be passed to many other objects, which themselves may “die” leaving the data access object to continue.

Ibbo
__________________

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

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

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

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

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 07-11-2006, 05:23 PM Re: Multiple Classes?
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
Thank you for your help
timsquash5 is offline
Reply With Quote
View Public Profile
 
Old 07-25-2006, 09:21 AM Re: Multiple Classes?
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
I am now having a few problems with classes and mysql queries.

This is an example of the kind of thing i am doing, but it doesn't work:
PHP Code:
<?php
class class1
{
  var 
$variable  "1";  
}

class 
class2
{
    var 
$_obj;
    
    function 
class2($obj){
        
$this->_obj=$obj;
    }
    
    
functionquery()
    {  
        
$select_array mysql_query("SELECT * FROM `table` WHERE `message_id` <= '$this->_obj->variable'") or die(mysql_error());
        while (
$get_array mysql_fetch_array($select_array))
        {
        echo 
$get_array['message']."<br>";
        }
    }
}
$class1 = new class1();
$class2 = new class2($class1);
$variable $class2->query();
?>
To get it to work i have to:

PHP Code:
<?php
class class1
{
  var 
$variable  "1";  
}

class 
class2
{
    var 
$_obj;
    
    function 
class2($obj){
        
$this->_obj=$obj;
    }
    
    
functionquery()
    {  
        
$variable $this->_obj->variable;
        
$select_array mysql_query("SELECT * FROM `table` WHERE `message_id` <= '$variable'") or die(mysql_error());
        while (
$get_array mysql_fetch_array($select_array))
        {
        echo 
$get_array['message']."<br>";
        }
    }
}
$class1 = new class1();
$class2 = new class2($class1);
$variable $class2->query();
?>
Why is that?

Last edited by timsquash5; 07-25-2006 at 09:22 AM..
timsquash5 is offline
Reply With Quote
View Public Profile
 
Old 07-26-2006, 06:03 AM Re: Multiple Classes?
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Try passing it like '{$this->_obj->variable}'

Failing that I dont know why its failing in method 1. However there is nothing wrong with option 2 method either (which is good practice too as working directly on vars from another class may cause mayhem if others are using those vars too).

It gets a bit complex but basically copy remote var into a local one. Work on the local one then write it back should you need to.

I know this example of yours does not need that sort of thing to happen but if you start to practice it now you will be better off when dealing with bigger apps.

Ibbo
__________________

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

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

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

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

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf

Last edited by ibbo; 07-26-2006 at 06:05 AM..
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 07-26-2006, 06:09 PM Re: Multiple Classes?
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
Thanks again Works.
timsquash5 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Multiple Classes?
 

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.17459 seconds with 12 queries