Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18730 > unrolled thread

ArrayObject vs array

Started byalex <1j9448a02@lnx159sneakemail.com.invalid>
First post2021-07-23 11:16 +0200
Last post2021-07-26 12:16 +0200
Articles 11 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  ArrayObject vs array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-23 11:16 +0200
    Re: ArrayObject vs array Jerry Stuckle <jstucklex@attglobal.net> - 2021-07-23 13:39 -0400
      Re: ArrayObject vs array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-24 08:26 +0200
        Re: ArrayObject vs array "J.O. Aho" <user@example.net> - 2021-07-24 12:10 +0200
          Re: ArrayObject vs array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-24 13:05 +0200
            Re: ArrayObject vs array Jerry Stuckle <jstucklex@attglobal.net> - 2021-07-24 12:56 -0400
              Re: ArrayObject vs array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-25 11:13 +0200
                Re: ArrayObject vs array "J.O. Aho" <user@example.net> - 2021-07-25 11:48 +0200
                  Re: ArrayObject vs array alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-26 10:14 +0200
                    Re: ArrayObject vs array "J.O. Aho" <user@example.net> - 2021-07-26 11:11 +0200
                    Re: ArrayObject vs array Arno Welzel <usenet@arnowelzel.de> - 2021-07-26 12:16 +0200

#18730 — ArrayObject vs array

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-23 11:16 +0200
SubjectArrayObject vs array
Message-ID<sde1e5$p89$1@gioia.aioe.org>
class C implements ArrayAccess, Countable, IteratorAggregate, Serializable {

     private $container = ['a', 'b'];

     public function offsetSet($offset, $value) {
         if (is_null($offset)) {
             $this->container[] = $value;
         } else {
             $this->container[$offset] = $value;
         }
     }

     public function offsetExists($offset) {
         return isset($this->container[$offset]);
     }

     public function offsetUnset($offset) {
         unset($this->container[$offset]);
     }

     public function offsetGet($offset) {
         return isset($this->container[$offset]) ? 
$this->container[$offset] : null;
     }

     public function count() {
         return count($this->container);
     }

     public function getIterator() {
         return new ArrayIterator($this->container);
     }

     public function serialize() {
         return serialize($this->container);
     }

     public function unserialize($data) {
         $this->container = unserialize($data);
     }

     public function getData() {
         return $this->container;
     }

}

print_r(
         (array) new ArrayObject(['a', 'b'])
);
print_r(
         (array) new C
);

Output:

Array
(
     [0] => a
     [1] => b
)
Array
(
     [Ccontainer] => Array
         (
             [0] => a
             [1] => b
         )

)

Because the result is not the same?

[toc] | [next] | [standalone]


#18732

FromJerry Stuckle <jstucklex@attglobal.net>
Date2021-07-23 13:39 -0400
Message-ID<sdeuru$3h5$1@jstuckle.eternal-september.org>
In reply to#18730
On 7/23/2021 5:16 AM, alex wrote:
> class C implements ArrayAccess, Countable, IteratorAggregate, 
> Serializable {
> 
>      private $container = ['a', 'b'];
> 
>      public function offsetSet($offset, $value) {
>          if (is_null($offset)) {
>              $this->container[] = $value;
>          } else {
>              $this->container[$offset] = $value;
>          }
>      }
> 
>      public function offsetExists($offset) {
>          return isset($this->container[$offset]);
>      }
> 
>      public function offsetUnset($offset) {
>          unset($this->container[$offset]);
>      }
> 
>      public function offsetGet($offset) {
>          return isset($this->container[$offset]) ? 
> $this->container[$offset] : null;
>      }
> 
>      public function count() {
>          return count($this->container);
>      }
> 
>      public function getIterator() {
>          return new ArrayIterator($this->container);
>      }
> 
>      public function serialize() {
>          return serialize($this->container);
>      }
> 
>      public function unserialize($data) {
>          $this->container = unserialize($data);
>      }
> 
>      public function getData() {
>          return $this->container;
>      }
> 
> }
> 
> print_r(
>          (array) new ArrayObject(['a', 'b'])
> );
> print_r(
>          (array) new C
> );
> 
> Output:
> 
> Array
> (
>      [0] => a
>      [1] => b
> )
> Array
> (
>      [Ccontainer] => Array
>          (
>              [0] => a
>              [1] => b
>          )
> 
> )
> 
> Because the result is not the same?

An object is not the same as a basic type.  A basic type holds one or 
more values.  An object can also have methods to operate on those 
values.  The two are not compatible.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#18734

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-24 08:26 +0200
Message-ID<sdgbps$18kp$1@gioia.aioe.org>
In reply to#18732
Il 23/07/21 19:39, Jerry Stuckle ha scritto:
> On 7/23/2021 5:16 AM, alex wrote:
>> class C implements ArrayAccess, Countable, IteratorAggregate, 
>> Serializable {
>>
>>      private $container = ['a', 'b'];
>>
>>      public function offsetSet($offset, $value) {
>>          if (is_null($offset)) {
>>              $this->container[] = $value;
>>          } else {
>>              $this->container[$offset] = $value;
>>          }
>>      }
>>
>>      public function offsetExists($offset) {
>>          return isset($this->container[$offset]);
>>      }
>>
>>      public function offsetUnset($offset) {
>>          unset($this->container[$offset]);
>>      }
>>
>>      public function offsetGet($offset) {
>>          return isset($this->container[$offset]) ? 
>> $this->container[$offset] : null;
>>      }
>>
>>      public function count() {
>>          return count($this->container);
>>      }
>>
>>      public function getIterator() {
>>          return new ArrayIterator($this->container);
>>      }
>>
>>      public function serialize() {
>>          return serialize($this->container);
>>      }
>>
>>      public function unserialize($data) {
>>          $this->container = unserialize($data);
>>      }
>>
>>      public function getData() {
>>          return $this->container;
>>      }
>>
>> }
>>
>> print_r(
>>          (array) new ArrayObject(['a', 'b'])
>> );
>> print_r(
>>          (array) new C
>> );
>>
>> Output:
>>
>> Array
>> (
>>      [0] => a
>>      [1] => b
>> )
>> Array
>> (
>>      [Ccontainer] => Array
>>          (
>>              [0] => a
>>              [1] => b
>>          )
>>
>> )
>>
>> Because the result is not the same?
> 
> An object is not the same as a basic type.  A basic type holds one or 
> more values.  An object can also have methods to operate on those 
> values.  The two are not compatible.
> 

Could you tell me what these methods are?

[toc] | [prev] | [next] | [standalone]


#18742

From"J.O. Aho" <user@example.net>
Date2021-07-24 12:10 +0200
Message-ID<im277qF7fbjU1@mid.individual.net>
In reply to#18734
On 24/07/2021 08.26, alex wrote:
> Il 23/07/21 19:39, Jerry Stuckle ha scritto:
>> On 7/23/2021 5:16 AM, alex wrote:
>>> class C implements ArrayAccess, Countable, IteratorAggregate, 
>>> Serializable {
>>>
>>>      private $container = ['a', 'b'];
>>>
>>>      public function offsetSet($offset, $value) {
>>>          if (is_null($offset)) {
>>>              $this->container[] = $value;
>>>          } else {
>>>              $this->container[$offset] = $value;
>>>          }
>>>      }
>>>
>>>      public function offsetExists($offset) {
>>>          return isset($this->container[$offset]);
>>>      }
>>>
>>>      public function offsetUnset($offset) {
>>>          unset($this->container[$offset]);
>>>      }
>>>
>>>      public function offsetGet($offset) {
>>>          return isset($this->container[$offset]) ? 
>>> $this->container[$offset] : null;
>>>      }
>>>
>>>      public function count() {
>>>          return count($this->container);
>>>      }
>>>
>>>      public function getIterator() {
>>>          return new ArrayIterator($this->container);
>>>      }
>>>
>>>      public function serialize() {
>>>          return serialize($this->container);
>>>      }
>>>
>>>      public function unserialize($data) {
>>>          $this->container = unserialize($data);
>>>      }
>>>
>>>      public function getData() {
>>>          return $this->container;
>>>      }
>>>
>>> }
>>>
>>> print_r(
>>>          (array) new ArrayObject(['a', 'b'])
>>> );
>>> print_r(
>>>          (array) new C
>>> );
>>>
>>> Output:
>>>
>>> Array
>>> (
>>>      [0] => a
>>>      [1] => b
>>> )
>>> Array
>>> (
>>>      [Ccontainer] => Array
>>>          (
>>>              [0] => a
>>>              [1] => b
>>>          )
>>>
>>> )
>>>
>>> Because the result is not the same?
>>
>> An object is not the same as a basic type.  A basic type holds one or 
>> more values.  An object can also have methods to operate on those 
>> values.  The two are not compatible.
>>
> 
> Could you tell me what these methods are?

The object can be of any type, so the whole depends on the class definition.

for example:
class myclass {
	public $array = Array();
	public function getFirst() {
		if(sizeof($this->array) == 0)
		{
			return null;
		}
		return $this->array[1];
	}
}

this has the method getFirst.

-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#18743

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-24 13:05 +0200
Message-ID<sdgs5f$1suj$1@gioia.aioe.org>
In reply to#18742
Il 24/07/21 12:10, J.O. Aho ha scritto:
> On 24/07/2021 08.26, alex wrote:
>> Il 23/07/21 19:39, Jerry Stuckle ha scritto:
>>> On 7/23/2021 5:16 AM, alex wrote:
>>>> class C implements ArrayAccess, Countable, IteratorAggregate, 
>>>> Serializable {
>>>>
>>>>      private $container = ['a', 'b'];
>>>>
>>>>      public function offsetSet($offset, $value) {
>>>>          if (is_null($offset)) {
>>>>              $this->container[] = $value;
>>>>          } else {
>>>>              $this->container[$offset] = $value;
>>>>          }
>>>>      }
>>>>
>>>>      public function offsetExists($offset) {
>>>>          return isset($this->container[$offset]);
>>>>      }
>>>>
>>>>      public function offsetUnset($offset) {
>>>>          unset($this->container[$offset]);
>>>>      }
>>>>
>>>>      public function offsetGet($offset) {
>>>>          return isset($this->container[$offset]) ? 
>>>> $this->container[$offset] : null;
>>>>      }
>>>>
>>>>      public function count() {
>>>>          return count($this->container);
>>>>      }
>>>>
>>>>      public function getIterator() {
>>>>          return new ArrayIterator($this->container);
>>>>      }
>>>>
>>>>      public function serialize() {
>>>>          return serialize($this->container);
>>>>      }
>>>>
>>>>      public function unserialize($data) {
>>>>          $this->container = unserialize($data);
>>>>      }
>>>>
>>>>      public function getData() {
>>>>          return $this->container;
>>>>      }
>>>>
>>>> }
>>>>
>>>> print_r(
>>>>          (array) new ArrayObject(['a', 'b'])
>>>> );
>>>> print_r(
>>>>          (array) new C
>>>> );
>>>>
>>>> Output:
>>>>
>>>> Array
>>>> (
>>>>      [0] => a
>>>>      [1] => b
>>>> )
>>>> Array
>>>> (
>>>>      [Ccontainer] => Array
>>>>          (
>>>>              [0] => a
>>>>              [1] => b
>>>>          )
>>>>
>>>> )
>>>>
>>>> Because the result is not the same?
>>>
>>> An object is not the same as a basic type.  A basic type holds one or 
>>> more values.  An object can also have methods to operate on those 
>>> values.  The two are not compatible.
>>>
>>
>> Could you tell me what these methods are?
> 
> The object can be of any type, so the whole depends on the class 
> definition.
> 
> for example:
> class myclass {
>      public $array = Array();
>      public function getFirst() {
>          if(sizeof($this->array) == 0)
>          {
>              return null;
>          }
>          return $this->array[1];
>      }
> }
> 
> this has the method getFirst.
> 

???????
So what?

[toc] | [prev] | [next] | [standalone]


#18744

FromJerry Stuckle <jstucklex@attglobal.net>
Date2021-07-24 12:56 -0400
Message-ID<sdhgno$tt9$1@jstuckle.eternal-september.org>
In reply to#18743
On 7/24/2021 7:05 AM, alex wrote:
> Il 24/07/21 12:10, J.O. Aho ha scritto:
>> On 24/07/2021 08.26, alex wrote:
>>> Il 23/07/21 19:39, Jerry Stuckle ha scritto:
>>>> On 7/23/2021 5:16 AM, alex wrote:
>>>>> class C implements ArrayAccess, Countable, IteratorAggregate, 
>>>>> Serializable {
>>>>>
>>>>>      private $container = ['a', 'b'];
>>>>>
>>>>>      public function offsetSet($offset, $value) {
>>>>>          if (is_null($offset)) {
>>>>>              $this->container[] = $value;
>>>>>          } else {
>>>>>              $this->container[$offset] = $value;
>>>>>          }
>>>>>      }
>>>>>
>>>>>      public function offsetExists($offset) {
>>>>>          return isset($this->container[$offset]);
>>>>>      }
>>>>>
>>>>>      public function offsetUnset($offset) {
>>>>>          unset($this->container[$offset]);
>>>>>      }
>>>>>
>>>>>      public function offsetGet($offset) {
>>>>>          return isset($this->container[$offset]) ? 
>>>>> $this->container[$offset] : null;
>>>>>      }
>>>>>
>>>>>      public function count() {
>>>>>          return count($this->container);
>>>>>      }
>>>>>
>>>>>      public function getIterator() {
>>>>>          return new ArrayIterator($this->container);
>>>>>      }
>>>>>
>>>>>      public function serialize() {
>>>>>          return serialize($this->container);
>>>>>      }
>>>>>
>>>>>      public function unserialize($data) {
>>>>>          $this->container = unserialize($data);
>>>>>      }
>>>>>
>>>>>      public function getData() {
>>>>>          return $this->container;
>>>>>      }
>>>>>
>>>>> }
>>>>>
>>>>> print_r(
>>>>>          (array) new ArrayObject(['a', 'b'])
>>>>> );
>>>>> print_r(
>>>>>          (array) new C
>>>>> );
>>>>>
>>>>> Output:
>>>>>
>>>>> Array
>>>>> (
>>>>>      [0] => a
>>>>>      [1] => b
>>>>> )
>>>>> Array
>>>>> (
>>>>>      [Ccontainer] => Array
>>>>>          (
>>>>>              [0] => a
>>>>>              [1] => b
>>>>>          )
>>>>>
>>>>> )
>>>>>
>>>>> Because the result is not the same?
>>>>
>>>> An object is not the same as a basic type.  A basic type holds one 
>>>> or more values.  An object can also have methods to operate on those 
>>>> values.  The two are not compatible.
>>>>
>>>
>>> Could you tell me what these methods are?
>>
>> The object can be of any type, so the whole depends on the class 
>> definition.
>>
>> for example:
>> class myclass {
>>      public $array = Array();
>>      public function getFirst() {
>>          if(sizeof($this->array) == 0)
>>          {
>>              return null;
>>          }
>>          return $this->array[1];
>>      }
>> }
>>
>> this has the method getFirst.
>>
> 
> ???????
> So what?

It means that the ArrayObject is a built-in type allows an object to 
function as an array, as the PHP doc says.  Your user-defined object 
contains an array but does not function as an array, as the output from 
print_r() shows.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#18746

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-25 11:13 +0200
Message-ID<sdj9us$o4p$1@gioia.aioe.org>
In reply to#18744
Il 24/07/21 18:56, Jerry Stuckle ha scritto:
> On 7/24/2021 7:05 AM, alex wrote:
>> Il 24/07/21 12:10, J.O. Aho ha scritto:
>>> On 24/07/2021 08.26, alex wrote:
>>>> Il 23/07/21 19:39, Jerry Stuckle ha scritto:
>>>>> On 7/23/2021 5:16 AM, alex wrote:
>>>>>> class C implements ArrayAccess, Countable, IteratorAggregate, 
>>>>>> Serializable {
>>>>>>
>>>>>>      private $container = ['a', 'b'];
>>>>>>
>>>>>>      public function offsetSet($offset, $value) {
>>>>>>          if (is_null($offset)) {
>>>>>>              $this->container[] = $value;
>>>>>>          } else {
>>>>>>              $this->container[$offset] = $value;
>>>>>>          }
>>>>>>      }
>>>>>>
>>>>>>      public function offsetExists($offset) {
>>>>>>          return isset($this->container[$offset]);
>>>>>>      }
>>>>>>
>>>>>>      public function offsetUnset($offset) {
>>>>>>          unset($this->container[$offset]);
>>>>>>      }
>>>>>>
>>>>>>      public function offsetGet($offset) {
>>>>>>          return isset($this->container[$offset]) ? 
>>>>>> $this->container[$offset] : null;
>>>>>>      }
>>>>>>
>>>>>>      public function count() {
>>>>>>          return count($this->container);
>>>>>>      }
>>>>>>
>>>>>>      public function getIterator() {
>>>>>>          return new ArrayIterator($this->container);
>>>>>>      }
>>>>>>
>>>>>>      public function serialize() {
>>>>>>          return serialize($this->container);
>>>>>>      }
>>>>>>
>>>>>>      public function unserialize($data) {
>>>>>>          $this->container = unserialize($data);
>>>>>>      }
>>>>>>
>>>>>>      public function getData() {
>>>>>>          return $this->container;
>>>>>>      }
>>>>>>
>>>>>> }
>>>>>>
>>>>>> print_r(
>>>>>>          (array) new ArrayObject(['a', 'b'])
>>>>>> );
>>>>>> print_r(
>>>>>>          (array) new C
>>>>>> );
>>>>>>
>>>>>> Output:
>>>>>>
>>>>>> Array
>>>>>> (
>>>>>>      [0] => a
>>>>>>      [1] => b
>>>>>> )
>>>>>> Array
>>>>>> (
>>>>>>      [Ccontainer] => Array
>>>>>>          (
>>>>>>              [0] => a
>>>>>>              [1] => b
>>>>>>          )
>>>>>>
>>>>>> )
>>>>>>
>>>>>> Because the result is not the same?
>>>>>
>>>>> An object is not the same as a basic type.  A basic type holds one 
>>>>> or more values.  An object can also have methods to operate on 
>>>>> those values.  The two are not compatible.
>>>>>
>>>>
>>>> Could you tell me what these methods are?
>>>
>>> The object can be of any type, so the whole depends on the class 
>>> definition.
>>>
>>> for example:
>>> class myclass {
>>>      public $array = Array();
>>>      public function getFirst() {
>>>          if(sizeof($this->array) == 0)
>>>          {
>>>              return null;
>>>          }
>>>          return $this->array[1];
>>>      }
>>> }
>>>
>>> this has the method getFirst.
>>>
>>
>> ???????
>> So what?
> 
> It means that the ArrayObject is a built-in type allows an object to 
> function as an array, as the PHP doc says.  Your user-defined object 
> contains an array but does not function as an array, as the output from 
> print_r() shows.
> 

So there is no way to create an object (class) that can *also* function 
as an array?

[toc] | [prev] | [next] | [standalone]


#18747

From"J.O. Aho" <user@example.net>
Date2021-07-25 11:48 +0200
Message-ID<im4qbnFnmluU1@mid.individual.net>
In reply to#18746
On 25/07/2021 11.13, alex wrote:
> Il 24/07/21 18:56, Jerry Stuckle ha scritto:
>> On 7/24/2021 7:05 AM, alex wrote:
>>> Il 24/07/21 12:10, J.O. Aho ha scritto:
>>>> On 24/07/2021 08.26, alex wrote:
>>>>> Il 23/07/21 19:39, Jerry Stuckle ha scritto:
>>>>>> On 7/23/2021 5:16 AM, alex wrote:
>>>>>>> class C implements ArrayAccess, Countable, IteratorAggregate, 
>>>>>>> Serializable {
>>>>>>>
>>>>>>>      private $container = ['a', 'b'];
>>>>>>>
>>>>>>>      public function offsetSet($offset, $value) {
>>>>>>>          if (is_null($offset)) {
>>>>>>>              $this->container[] = $value;
>>>>>>>          } else {
>>>>>>>              $this->container[$offset] = $value;
>>>>>>>          }
>>>>>>>      }
>>>>>>>
>>>>>>>      public function offsetExists($offset) {
>>>>>>>          return isset($this->container[$offset]);
>>>>>>>      }
>>>>>>>
>>>>>>>      public function offsetUnset($offset) {
>>>>>>>          unset($this->container[$offset]);
>>>>>>>      }
>>>>>>>
>>>>>>>      public function offsetGet($offset) {
>>>>>>>          return isset($this->container[$offset]) ? 
>>>>>>> $this->container[$offset] : null;
>>>>>>>      }
>>>>>>>
>>>>>>>      public function count() {
>>>>>>>          return count($this->container);
>>>>>>>      }
>>>>>>>
>>>>>>>      public function getIterator() {
>>>>>>>          return new ArrayIterator($this->container);
>>>>>>>      }
>>>>>>>
>>>>>>>      public function serialize() {
>>>>>>>          return serialize($this->container);
>>>>>>>      }
>>>>>>>
>>>>>>>      public function unserialize($data) {
>>>>>>>          $this->container = unserialize($data);
>>>>>>>      }
>>>>>>>
>>>>>>>      public function getData() {
>>>>>>>          return $this->container;
>>>>>>>      }
>>>>>>>
>>>>>>> }
>>>>>>>
>>>>>>> print_r(
>>>>>>>          (array) new ArrayObject(['a', 'b'])
>>>>>>> );
>>>>>>> print_r(
>>>>>>>          (array) new C
>>>>>>> );
>>>>>>>
>>>>>>> Output:
>>>>>>>
>>>>>>> Array
>>>>>>> (
>>>>>>>      [0] => a
>>>>>>>      [1] => b
>>>>>>> )
>>>>>>> Array
>>>>>>> (
>>>>>>>      [Ccontainer] => Array
>>>>>>>          (
>>>>>>>              [0] => a
>>>>>>>              [1] => b
>>>>>>>          )
>>>>>>>
>>>>>>> )
>>>>>>>
>>>>>>> Because the result is not the same?
>>>>>>
>>>>>> An object is not the same as a basic type.  A basic type holds one 
>>>>>> or more values.  An object can also have methods to operate on 
>>>>>> those values.  The two are not compatible.
>>>>>>
>>>>>
>>>>> Could you tell me what these methods are?
>>>>
>>>> The object can be of any type, so the whole depends on the class 
>>>> definition.
>>>>
>>>> for example:
>>>> class myclass {
>>>>      public $array = Array();
>>>>      public function getFirst() {
>>>>          if(sizeof($this->array) == 0)
>>>>          {
>>>>              return null;
>>>>          }
>>>>          return $this->array[1];
>>>>      }
>>>> }
>>>>
>>>> this has the method getFirst.
>>>>
>>>
>>> ???????
>>> So what?
>>
>> It means that the ArrayObject is a built-in type allows an object to 
>> function as an array, as the PHP doc says.  Your user-defined object 
>> contains an array but does not function as an array, as the output 
>> from print_r() shows.
>>
> 
> So there is no way to create an object (class) that can *also* function 
> as an array?

- A class can have an variable that is an array.
- An array can hold objects.

You can make objects to have links to other objects (reference), then it 
would be a bit array like at the same time it can hold it's data, the 
problem will be that none of the array functions in PHP will work, so I 
think you need to pick one of the two options that have to use real arrays.

-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#18751

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-26 10:14 +0200
Message-ID<sdlqtr$i7a$1@gioia.aioe.org>
In reply to#18747
Il 25/07/21 11:48, J.O. Aho ha scritto:
> 
> You can make objects to have links to other objects (reference),

That is?

> then it 
> would be a bit array like at the same time it can hold it's data

That is?

[toc] | [prev] | [next] | [standalone]


#18752

From"J.O. Aho" <user@example.net>
Date2021-07-26 11:11 +0200
Message-ID<im7ci0F9987U1@mid.individual.net>
In reply to#18751
On 26/07/2021 10.14, alex wrote:
> Il 25/07/21 11:48, J.O. Aho ha scritto:
>>
>> You can make objects to have links to other objects (reference),
> 
> That is?

class myclass {
	public $next;
	public $data;
}

$a = new myclass();
$a->next = new myclass();
$a->next->next = new myclass();


>> then it would be a bit array like at the same time it can hold it's data
> 
> That is?

$a->data = "my data";
$a->next->data = "next cell data";

-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#18754

FromArno Welzel <usenet@arnowelzel.de>
Date2021-07-26 12:16 +0200
Message-ID<im7gccFa1nlU1@mid.individual.net>
In reply to#18751
alex:

> Il 25/07/21 11:48, J.O. Aho ha scritto:
>>
>> You can make objects to have links to other objects (reference),
> 
> That is?

<?php
$a = new Object();

// $b is a new reference ("link") to the object in $a and not(!) a copy

$b = $a;

// Now we change something in the object

$b->setName('foobar');

// This will return the same name wich was just set using $b since
// $b is just another reference to the object originally assinged to $a

$a->getName();
?>

If you need a copy of an object, you need to use the "clone" keyword:

<?php
$a = new Object();

// This will create a copy of the object in $b
$b = clone $a;
?>



-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web