TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-30-2007, 01:38 PM   #1 (permalink)
The Wanderer
Newcomer 
 
Join Date: Nov 2007
Posts: 14
Thanks: 1
MartynMJ is on a distinguished road
Default Is referencing a class bad practice?

Just wondering is referencing a class in another class bad? Like so

PHP Code:
<?php

class test
{
   var 
$testing "";

   function 
blah()
   {
      return 
$this->testing;
   }
}

?>
And then in another file

PHP Code:
<?php

//include the file with the class above in

class two
{
   var 
$test "";

   function 
two ( &$test )
   {
      
$this->test $test;
   }

   function 
again ()
   {
      
$data $this->test->blah();
      return 
$data;
   }

}

?>
Hope that makes sense? :p
MartynMJ is offline  
Reply With Quote
Old 11-30-2007, 01:48 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

No, not at all. Passing in objects is perfectly fine and allows you to place varying logic into separate classes. What you have there is just a simplified example of what was being spoke about in the interfaces tutorial, err, tutorials. Geez! How many do we have?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-30-2007, 02:41 PM   #3 (permalink)
The Wanderer
Newcomer 
 
Join Date: Nov 2007
Posts: 14
Thanks: 1
MartynMJ is on a distinguished road
Default

Heh, Thats good to know.

Just wondering though. Could the way the server's configured make it act differently?

I mean i've developed a script locally and it works fine then i've put it on a web server and its not working properly.

how i mean is i'm referencing a class and then in a function passing some data to an array in the referenced class.

Then back on the main page it called to a functio on the class that was passed data to and the data doesn't seem to be there.

Any idea why it would do this?
MartynMJ is offline  
Reply With Quote
Old 11-30-2007, 02:43 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

The statement "Then back on the main page", sounds like you're trying to keep the class data alive during page reloads, is this the case?
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-30-2007, 07:04 PM   #5 (permalink)
The Wanderer
Newcomer 
 
Join Date: Nov 2007
Posts: 14
Thanks: 1
MartynMJ is on a distinguished road
Default

No, Here's an example.

index.php
PHP Code:
<?php
require_once ( "class1.php" );
require_once ( 
"class2.php" );

$class1 = new class1;
$class2 = new class2 $class1 );

$class2->blah();

echo 
$class1->execute();

?>
class1.php
PHP Code:
<?php

class class1
{
   var 
$data "";
   
   function 
execute()
   {
      return 
$this->data;
   }

}

?>
class2.php
PHP Code:
<?php

class class2
{
   var 
$class1 "";
   
   function 
class2 ( &$class1 )
   {
       
$this->class1 $class1;
   }

   function 
blah ()
   {
      
$this->class1->data "TESTING";
   }

}

?>
Now on index.php it should echo TESTING right? But this doesn't appear to be working on a server i'm hosting the site on
MartynMJ is offline  
Reply With Quote
Old 11-30-2007, 09:26 PM   #6 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

That is very strange, what version of PHP is the server using?
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-30-2007, 11:25 PM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Tis indeed strange, it works fine for me
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-30-2007, 11:35 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This thread doesn't need another "me too", but it's going to get one anyway! It works for me as well. I guess it all comes to the PHP version, although even still it's still very bizarre.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-01-2007, 12:14 AM   #9 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Pre-PHP 5 when you refer to a class in a function call, without using the ampersand operator then PHP would clone the class and not simply pass a reference as you are hoping for.

your code:
PHP Code:
<?php
require_once ( "class1.php" );
require_once ( 
"class2.php" );

$class1 = new class1;
$class2 = new class2 $class1 );

$class2->blah();

echo 
$class1->execute();

?>
Should be:
PHP Code:
$class2 = new class2 ( &$class1 ); 
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 12-01-2007, 01:55 AM   #10 (permalink)
The Wanderer
Newcomer 
 
Join Date: Nov 2007
Posts: 14
Thanks: 1
MartynMJ is on a distinguished road
Default

Quote:
Originally Posted by bluesaga View Post
Pre-PHP 5 when you refer to a class in a function call, without using the ampersand operator then PHP would clone the class and not simply pass a reference as you are hoping for.

your code:
PHP Code:
<?php
require_once ( "class1.php" );
require_once ( 
"class2.php" );

$class1 = new class1;
$class2 = new class2 $class1 );

$class2->blah();

echo 
$class1->execute();

?>
Should be:
PHP Code:
$class2 = new class2 ( &$class1 ); 
First of all PHP Version 4.4.7.

And i tried adding the ampersand but still got the same outcome. As this needs to be done soon i have worked around it by returning the data and then calling to the template functions but this was quite a pain :p

Thanks all
MartynMJ is offline  
Reply With Quote
Old 12-01-2007, 02:15 AM   #11 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

There's a very simple solution.

Change: (in the class2 constructor)
PHP Code:
$this->class1 $class1
To:
PHP Code:
$this->class1 =& $class1
Why does this work? It's all about the way that PHP4 handles passing around references to objects. The &$class1 argument for the constructor ensured that the argument was brought in by reference, but when it was being assigned to the class1 property the assignment was by value -- a copy of the object was made and assigned to the property.

When we changed the data property, it was only changing it in the copy and not the original. Using =& the variable is assigned by reference as we intended. Hopefully you can see now why this behaviour was changed in PHP5 to assign by reference by default. :)
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
MartynMJ (12-01-2007)
Old 12-01-2007, 02:26 AM   #12 (permalink)
The Wanderer
Newcomer 
 
Join Date: Nov 2007
Posts: 14
Thanks: 1
MartynMJ is on a distinguished road
Smile

Quote:
Originally Posted by Salathe View Post
There's a very simple solution.

Change: (in the class2 constructor)
PHP Code:
$this->class1 $class1
To:
PHP Code:
$this->class1 =& $class1
Why does this work? It's all about the way that PHP4 handles passing around references to objects. The &$class1 argument for the constructor ensured that the argument was brought in by reference, but when it was being assigned to the class1 property the assignment was by value -- a copy of the object was made and assigned to the property.

When we changed the data property, it was only changing it in the copy and not the original. Using =& the variable is assigned by reference as we intended. Hopefully you can see now why this behaviour was changed in PHP5 to assign by reference by default. :)


Works perfectly. Greatly appriciated :D.

Thanks very much :)
MartynMJ is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


All times are GMT. The time now is 10:51 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design