TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Is referencing a class bad practice? (http://www.talkphp.com/general/1556-referencing-class-bad-practice.html)

MartynMJ 11-30-2007 01:38 PM

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

Wildhoney 11-30-2007 01:48 PM

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? :-)

MartynMJ 11-30-2007 02:41 PM

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?

Karl 11-30-2007 02:43 PM

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?

MartynMJ 11-30-2007 07:04 PM

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

Karl 11-30-2007 09:26 PM

That is very strange, what version of PHP is the server using?

sketchMedia 11-30-2007 11:25 PM

Tis indeed strange, it works fine for me

Wildhoney 11-30-2007 11:35 PM

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.

bluesaga 12-01-2007 12:14 AM

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 ); 


MartynMJ 12-01-2007 01:55 AM

Quote:

Originally Posted by bluesaga (Post 4934)
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 ^^

Salathe 12-01-2007 02:15 AM

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. :)

MartynMJ 12-01-2007 02:26 AM

Quote:

Originally Posted by Salathe (Post 4947)
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 :)


All times are GMT. The time now is 09:13 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0