Ask a Question related to PHP Development, Design and Development.
-
kajaman (a.k.a. Hubert Łępicki ;)) #1
Diffrence between &new Object and new Object...
I must confess that I don't see the diffrence between using:
$MyObject = new Object();
and
$MyObject = &new Object();
Both expressions seem to produce new object and assign it to
$MyObject... Is there any reason to use "&new" instead of "new"?
kajaman (a.k.a. Hubert Łępicki ;)) Guest
-
Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object
I've created user controls that contain listboxes that are dynamically populated from the database. In the html view of the user control... -
webservice.htc and custom object array. only first object is deserialized in the result.value
Hello I've been using webservice.htc for 6 months or so with great results. recently i came into the following problem that I am not sure how to... -
Custom Control Problem :: Object reference not set to an instance of an object
Hi All! I have the following Custom Control file... '########### WebUserControl1.ascx ############# <%@ Control Language="vb"... -
[WebMethod] System.NullReferenceException: Object reference not set to an instance of an object.
Um, this isn't going to work, generally. Web services, as any web app (especially on Windows server 2003) are heavily sandboxed. The method you... -
Cannot serialize object of type System.Object[,]. Multidimensional arrays are not supported
Hi, I get this on server when trying to retun a 2 dim array. I apprecaite that they are not supported as per... -
Steve #2
Re: Diffrence between &new Object and new Object...
There is no &new. The syntax is actually...
$MyObject =& new Object();
where =& is an assignment operator that creates a reference.
To see the difference, try this...
// example class
class MyClass
{
var $m_strValue;
function MyClass()
{
// initialise
$this->m_strValue = -1;
}
function SetValue( $p_strNewValue )
{
$this->m_strValue = $p_strNewValue;
}
function GetValue()
{
return $this->m_strValue;
}
}
$objMyObject1 = new MyClass();
print $objMyObject1->GetValue() . "\n";
// -1
$objMyObject1->SetValue(2);
print $objMyObject1->GetValue() . "\n";
// 2
$objMyObject2 =& new MyClass();
print $objMyObject1->GetValue() . "\n";
// 2 - would have been -1 if I hadn't created a reference!
See [url]http://www.php.net/manual/en/language.references.php[/url]
---
Steve
Steve Guest
-
kajaman (a.k.a. Hubert Łępicki ;)) #3
Re: Diffrence between &new Object and new Object...
Steve wrote:
[cut]> There is no &new. The syntax is actually...
>
> $MyObject =& new Object();
>
> where =& is an assignment operator that creates a reference.
>
> To see the difference, try this...
>
> // example class
> class MyClass
> {
> var $m_strValue;
> function MyClass()
> {
> // initialise
> $this->m_strValue = -1;
> }
> function SetValue( $p_strNewValue )
> {
> $this->m_strValue = $p_strNewValue;
> }
> function GetValue()
> {
> return $this->m_strValue;
> }
> }
>
> $objMyObject1 = new MyClass();
> print $objMyObject1->GetValue() . "\n";
>
> // -1
>
> $objMyObject1->SetValue(2);
> print $objMyObject1->GetValue() . "\n";
>
> // 2
>
>
> $objMyObject2 =& new MyClass();
> print $objMyObject1->GetValue() . "\n";
about the last line: shouldn't it be:
print $objMyObject2->GetValue(). "\n";
?
Ok, I will make some experiments, and I'll see how it works.
kajaman (a.k.a. Hubert Łępicki ;)) Guest
-
kajaman (a.k.a. Hubert Łępicki ;)) #4
Re: Diffrence between &new Object and new Object...
Ok, but I can't see any difference! It doesn't matter if I change = into
=& , printed values are the same!
I use PHP 5 - maybe this is the reason?
kajaman (a.k.a. Hubert Łępicki ;)) Guest
-
Steve #5
Re: Diffrence between &new Object and new Object...
Oh dear. Yup, it should be. And, yup, when you correct that code> about the last line: shouldn't it be:
> print $objMyObject2->GetValue(). "\n";
there's no difference between the two objects. Oh dear.
---
Steve
Steve Guest
-
Hilarion #6
Re: Diffrence between &new Object and new Object...
The difference is:
$MyObject = new Object();
does:
1. creates Object
2. makes a copy of it (not using the class standard constructor)
3. assigns the copy to $MyObject variable
$MyObject = & new Object();
does:
1. creates Object
2. assigns the object to $MyObject variable
Why? Cause "=" is a copying operator. When you use "= &" then you
get reference copied.
Example below shows the difference (you get "oryginal" objects in $instances array,
and copy in $instance variable). On PHP v 4.3.7 I get this result:
$instance_counter: 0
$instance = new MyClass();
$instance_counter: 1
$instance->name: default name
$instances[count($instances)-1]->name: default name
$instance->name = "some name";
$instance->name: some name
$instances[count($instances)-1]->name: default name
$instance = & new MyClass();
$instance_counter: 2
$instance->name: default name
$instances[count($instances)-1]->name: default name
$instance->name = "other name";
$instance->name: other name
$instances[count($instances)-1]->name: other name
Hilarion
<?php
$instance_counter = 0;
$instances = array();
class MyClass {
var $name;
function MyClass() {
global $instances, $instance_counter;
$this->name = 'default name';
$instances[] = &$this;
$instance_counter++;
}
}
echo '$instance_counter: ' . $instance_counter . "\r\n\r\n";
echo '$instance = new MyClass();'."\r\n";
$instance = new MyClass();
echo '$instance_counter: ' . $instance_counter . "\r\n";
echo '$instance->name: ' . $instance->name . "\r\n";
echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n";
echo '$instance->name = "some name";'."\r\n";
$instance->name = "some name";
echo '$instance->name: ' . $instance->name . "\r\n";
echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n\r\n";
echo '$instance = & new MyClass();'."\r\n";
$instance = & new MyClass();
echo '$instance_counter: ' . $instance_counter . "\r\n";
echo '$instance->name: ' . $instance->name . "\r\n";
echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n";
echo '$instance->name = "other name";'."\r\n";
$instance->name = "other name";
echo '$instance->name: ' . $instance->name . "\r\n";
echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n\r\n";
?>
Hilarion Guest
-
mmLiu #7
Re: Diffrence between &new Object and new Object...
My php version is Version 5.3.0,and the result is :
$instance_counter: 0
$instance = new MyClass();
$instance_counter: 1
$instance->name: default name
$instances[count($instances)-1]->name: default name
$instance->name = "some name";
$instance->name: some name
$instances[count($instances)-1]->name: some name
$instance = & new MyClass();
$instance_counter: 2
$instance->name: default name
$instances[count($instances)-1]->name: default name
$instance->name = "other name";
$instance->name: other name
$instances[count($instances)-1]->name: other namemmLiu Guest
-
tamara30 #8
Re: Diffrence between &new Object and new Object...
HELLO
how are you doing today? i hope all is well.
My name is Miss Sonia emmedy i saw your profile today in[www.justskins.com]and became interested in you,l will also like to know you the more,pls contact me in my private email so l will give you my picture for you to know whom l am. Here is my email address (soniaemmedy3@hotmail.com) I believe we can move from here! I am waiting for your mail to my email address above. Miss Sonia (Remember the distance or color does not matter but love matters a lot in life)
Junior Member
- Join Date
- Mar 2011
- Location
- liberan
- Posts
- 4



Reply With Quote

