473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What is the difference between these two class ? [OOP]

I can't understand what is the specialty of & while naming a function. I am not good enough in OOP of PHP.

[PHP]class FirstClass{
function &FunctionName($Para)
{
return $Para; //Or any other operation
}
}
class SecondClass{
function FunctionName($Para)
{
return $Para; //Or any other operation
}
}
$UseClass = &FirstClass::FunctionName(10);
echo $UseClass;
$UseSecondClass = SecondClass::FunctionName(10);
echo $UseSecondClass;
[/PHP]

Output are same for both classes.
May 21 '08 #1
6 1356
Markus
6,050 Expert 4TB
You use the & operator to pass by reference.

Read about it here
May 21 '08 #2
TheServant
1,168 Expert 1GB
markus posted the same link, ignore this post. Took too long to reply...
May 21 '08 #3
coolsti
310 100+
Because of a post yesterday, I spent some time reading about the use of the & operator in PHP, and find it can be a bit confusing.

I knew about passing arguments by references in function calls, but not about assigning class objects as references with =& rather than the usual assignement with = alone. So I have never used this in any of my code.

But if I am correct, this makes little difference in my scripts. PHP is efficient in the way that when an object is copied using = rather than referred to with =&, no extra memory is used unless the object is modified in some way. So for my scripts, I do not think the use of = rather than =& makes a big difference. Or can someone tell me something to the contrary?

In my scripts, I have a "table" class that builds up the html for the page, and the table class can contain tables which contain tables. (This is not a memory efficient way to prepare the html output of the script, but by doing it this way, it is extremely easy to set up the page and the classes ensure proper matching of all table, tr and td tags). But I had been concerned that the "=" assignment of tables to other tables for complex pages would eventually eat up all available memory. I found out by examining the used memory after each assignment that this was not the case; assigning a table object to a class variable of another table object with = did not increase the memory at all. Very fortunate.
May 21 '08 #4
Atli
5,058 Expert 4TB
PHP is extremely cleaver when it comes to copying variables.

Even when you use the = operator to copy a variable, PHP will not replicate the variable in the memory until either copy of the variable changes. Until then the second copy will remain a reference to the first one.

Creating class instances by reference, by using =$, is deprecated in PHP5. The new keyword will do this automatically.
So, in PHP5:
Expand|Select|Wrap|Line Numbers
  1. // These are identical
  2. $first =& new fooClass();
  3. $first = new fooClass();
  4.  
It is noted in the manual.
Since PHP 5, new return reference automatically so using =& in this context is deprecated and produces E_STRICT level message.
May 21 '08 #5
coolsti
310 100+
Excellant! So all my scripts will then automatically become correct when I migrate over to PHP5!

:))
May 21 '08 #6
dlite922
1,584 Expert 1GB
PHP is extremely cleaver when it comes to copying variables.

Even when you use the = operator to copy a variable, PHP will not replicate the variable in the memory until either copy of the variable changes. Until then the second copy will remain a reference to the first one.

Creating class instances by reference, by using =$, is deprecated in PHP5. The new keyword will do this automatically.
So, in PHP5:
Expand|Select|Wrap|Line Numbers
  1. // These are identical
  2. $first =& new fooClass();
  3. $first = new fooClass();
  4.  
It is noted in the manual.
I actually found this out by mistake. It looked like voodoo had its spell on my code, so created a test program and turns out this was exactly true.

Pass by reference is important though when your passing objects. I use it in my DAO classe so they don't have to create and return a new object. it just modifies the current object and i can continue using it.

Cheers,

Dan
May 22 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
24
by: Xah Lee | last post by:
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
18
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an...
18
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) {...
13
by: Hussein B | last post by:
Hi, I'm familiar with static method concept, but what is the class method? how it does differ from static method? when to use it? -- class M: def method(cls, x): pass method =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.