Can someone please explain the results below
We switched to PHP 5
if I make a copy of an object, and then change the variable inside the
object the change is reflected in the copy.
Either the copy is a reference to the object or the variable inside
the object is a static variable, I'm not sure what is going on, but It
doesn't seem like it should be doing that. Is that because of a
setting on the server or something ?
<?
$label=new myObj();
$label->settext('the text');
$copy=$label;
print_r($copy);
$label->settext('diffe rent text');
print_r($copy);
class myObj{
var $text;
function settext($text){
$this->text=$text;
}
}
?>
results:
myObj Object
(
[text] =the text
)
myObj Object
(
[text] =different text
) 3 1609
On Oct 25, 11:50 am, grou...@reenie. org wrote:
Can someone please explain the results below
We switched to PHP 5
if I make a copy of an object, and then change the variable inside the
object the change is reflected in the copy.
Either the copy is a reference to the object or the variable inside
the object is a static variable, I'm not sure what is going on, but It
doesn't seem like it should be doing that. Is that because of a
setting on the server or something ?
<?
$label=new myObj();
$label->settext('the text');
$copy=$label;
print_r($copy);
$label->settext('diffe rent text');
print_r($copy);
class myObj{
var $text;
function settext($text){
$this->text=$text;
}
}
?>
results:
myObj Object
(
[text] =the text
)
myObj Object
(
[text] =different text
)
All objects in PHP 5, if I'm not mistaken, are always passed by
reference. Example:
<?php
class MyClass
{
public $myVar;
function __construct()
{
$this->myVar = 1;
}
}
$class1 = new MyClass();
$class2 = $class1;
$class1->myVar = 2;
echo $class2->myVar; // Outputs 2
?>
If you want to make a copy of an object, so its a new object and not a
reference is to use the clone keyword like this:
<?
class MyClass
{
public $myVar;
function __construct()
{
$this->myVar = 1;
}
}
$class1 = new MyClass();
$class2 = clone $class1;
$class1->myVar = 2;
echo $class2->myVar;
?>
You can also explicitly code what is to happen when an object is
cloned by using the __clone() class function. You can read more here: http://us3.php.net/manual/en/language.oop5.cloning.php
Carmony gr*****@reenie. org schrieb:
On Oct 25, 2:05 pm, JustinCarmony <carm...@gmail. comwrote:
>You can also explicitly code what is to happen when an object is cloned by using the __clone() class function.
$label=new geocellobj();
$label->setalign('righ t');
array($label->settext('uniqu e test'), $label->settext('diffe rent
text));
I don't think cloning will help me.
Well, just clone the objects before storing them into the array:
array(clone $label->settext(...) , ...)
On Oct 25, 6:17 pm, Thomas Hamacher <da...@nurfuers pam.dewrote:
grou...@reenie. org schrieb:
On Oct 25, 2:05 pm, JustinCarmony <carm...@gmail. comwrote:
You can also explicitly code what is to happen when an object is
cloned by using the __clone() class function.
$label=new geocellobj();
$label->setalign('righ t');
array($label->settext('uniqu e test'), $label->settext('diffe rent
text));
I don't think cloning will help me.
Well, just clone the objects before storing them into the array:
array(clone $label->settext(...) , ...)
Ok thanks that works great. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Edward Diener |
last post by:
Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor creates
an object from a copy of another object of the same kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
by: Tony Johansson |
last post by:
Hello!
I'm reading in a book about C++ and that is something that sound strange.
It says "Pointers have reference-assignment semantics similar to those in
Java. For example, after the assignment
Student* john = michael;
both john and michael share the same object. This type of an assignment is
different then value-assignmnet semantics used by class variables, as in
|
by: Kevin |
last post by:
Hi all,
I've got a PHP4 app that I developed which I'm trying to get to run on a
PHP5 server. Everything works great, except for one thing.
There's a particular routine that creates an original object, then copies
it. (The object constructor gets some meta information from the database, so
I copy it for performance reasons). The routine then modifies the copies.
PHP5 copies by reference by default, so this doesn't work--- I'm not
|
by: Jesper |
last post by:
Hi,
Does the concept "copy constructor" from c++ excist in
c#. What is the syntax.
best regards Jesper.
|
by: |
last post by:
any body please help me ,Please explain what is a shallow copy and what is a
deep copy?
| |
by: lion |
last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a
pointer will add to B, but if i want to create a copy of B instead of
pointer, how to operate?
Note:serialization isn't permitted
3x
|
by: blangela |
last post by:
3.0 Advanced Topic Addendum
There are a few cases where the C++ compiler cannot provide an
overloaded assignment operator for your class. If your class contains
a const member or/and a reference member, the compiler will not be able
to synthesize an assignment operator for your class. It actually helps
to think of a reference member as a const member (since it cannot be
made to reference any other object once it has been initialized).
...
|
by: JurgenvonOerthel |
last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and
Derived2 derive publicly from Base.
Given a 'const Base &input' I want to initialize a 'const Derived1
&output'.
If the dynamic type of 'input' is Derived1, then 'output' should
become a reference to 'input'.
Otherwise 'output' should become a reference to the (temporary) result
of the member function 'input.to_der1()' which returns a Derived1
object by value.
|
by: clicwar |
last post by:
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |