- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you pass objects by reference in PHP 5?
A PHP reference is an alias, that allows two different variables to write it to the same value. In PHP version 5, an object variable doesn't contain the object itself as its value. It holds an object identifier that allows object accessors to find the actual object.
When an object is sent by an argument, returned or assigned to a different variable, these different variables are not aliases. They contain a copy of the identifier, that points to the same object.
Example
$my_var = new class_name; echo $my_var->get_class_name(5)->value; $my_var->test(); echo $my_var->get_class_name(5)->value;
Output
This will produce the following output −
class_name #5
This isn't "pass by reference". It actually is "assignment by reference". In PHP 5 assignment by reference is the default behaviour with objects.
- Related Articles
- What is Pass By Reference and Pass By Value in PHP?
- How to pass reference parameters PHP?
- How do we pass parameters by reference in a C# method?
- Why do we pass a Pointer by Reference in C++?
- Pass by reference vs Pass by Value in java
- Describe pass by value and pass by reference in JavaScript?
- Is java pass by reference or pass by value?
- Differences between pass by value and pass by reference in C++
- How to pass an array by reference in C++
- How to pass arguments by reference in Python function?
- Pass an integer by reference in Java
- Pass by reference vs value in Python
- Is JavaScript a pass-by-reference or pass-by-value language?
- How to pass arguments by reference in a Python function?
- What is pass by reference in C language?

Advertisements