php-general Digest 30 Jan 2005 19:46:26 -0000 Issue 3258

Topics (messages 207706 through 207722):

Re: Is this a mysql_connect() bug?
        207706 by: Burhan Khalid
        207713 by: tom soyer

Re: Image Creation and Saving
        207707 by: hitek
        207719 by: NathanielGuy#21
        207721 by: Jochem Maas

question on getting URL
        207708 by: David Banning
        207709 by: Matthew Fonda

General question: packaging in PHP
        207710 by: Vivian Steller
        207711 by: Terje Sletteb�
        207712 by: Vivian Steller

Re: design pattern/code generators
        207714 by: Jochem Maas

Re: Permissions on uploaded image don't allow for over writing
        207715 by: Dave

Is this a bug?!!! I cna't believe! Sorry, if im wrong...
        207716 by: news.php.net
        207717 by: Marek Kilimajer
        207718 by: news.php.net
        207720 by: Jochem Maas
        207722 by: DvDmanDT

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message --- tom soyer wrote:
Thanks for the error handling code. I think PHP still has a basic
problem. If mysql sever connection times out because wrong username or
password was used, then mysql_connect() should return FALSE. But this
is not the case, why? Is it possible that mysql_connect() could not
read the "connection failed" message from mysql server?

I don't think this is case, because I thought that the problem could be that the script is timing out before MySQL can return a status code (possibly because MySQL is busy; network congestion, etc.)


However, after reading this:

"Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running."

I think the problem is not the script timing out, but rather it could be this:

"Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as mysql.default_host in your PHP configuration and leave the server field blank."
--- End Message ---
--- Begin Message ---
Thanks everyone for all the help. I found the problem. It has to do
with a bug in my error handler class that my script loaded prior to
executing mysql_connect(). Once I removed  the error handler class,
mysql_connect() started to behave as expected. My bad.

Tom


On Sun, 30 Jan 2005 01:16:10 -0600, Michael Sims
<[EMAIL PROTECTED]> wrote:
> tom soyer wrote:
> > Thanks for the error handling code. I think PHP still has a basic
> > problem. If mysql sever connection times out because wrong username or
> > password was used, then mysql_connect() should return FALSE.
> 
> It does, at least for me on PHP 4.3.10 connecting to a local MySQL 4.0.23
> server on Debian.  It returns a boolean false, and a warning is triggered:
> 
> Warning: mysql_connect(): Access denied for user: '[EMAIL PROTECTED]' (Using
> password: YES) in ...
> 
> If I give it a server name it cannot connect to (due to a firewall blocking
> the connection, for example) it will hang for about 60 seconds then return
> false, triggering a warning:
> 
> Warning: mysql_connect(): Can't connect to MySQL server on ...
> 
> I was going to suggest that you run a cli test script and trace the system
> calls to see what's going on but then I saw that you're using Windows.  I'm
> not sure if there is an strace/truss equivalent for it (anyone know)?
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
>

--- End Message ---
--- Begin Message --- Are you making sure the $_SESSION['user_sn'] folder exists before saving the image into it?
I'm asking because I originally thought it was a permission problem but php will tell you 'permission denied' if that was the case.



At 05:05 PM 1/29/2005, NathanielGuy#21 wrote:
Hello everyone,
I have been troubleshooting a problem with one of my scripts for a
while now, its purpose is to allow a user to upload a file, save it to
the server, resize it into a thumbnail, and save the thumbnail as
well.  In my script all goes well until it comes to saving the images,
the script throws these errors:

Warning: imagepng(): Unable to open
'/home/blacknut/public_html/picserv/picserv_mysql/pictures/blacknute/15.png'
for writing in /home/blacknut/public_html/picserv_mysql/addpic.php on
line 91

Warning: imagepng(): Unable to open
'/home/blacknut/public_html/picserv/picserv_mysql/pictures/blacknute/15t.png'
for writing in /home/blacknut/public_html/picserv_mysql/addpic.php on
line 95
Picture "ffq" was added to our database! The following is what we have
generated and it is what others will see

Here is the important part of the script, if more is needed i can post
it as well.

        if ($mime_type == 'jpeg') {
        $new_image = imagecreatefromjpeg($_FILES['new_image']['tmp_name']);
//Create an image refrence
        }
        elseif ($mime_type == 'png') {
        $new_image = imagecreatefrompng($_FILES['new_image']['tmp_name']);
//Create an image refrence
        }
        elseif ($mime_type == 'gif') {
        $new_image = imagecreatefromgif($_FILES['new_image']['tmp_name']);
//Create an image refrence
        }

  //Image Sizes
        list($new_image_width, $new_image_height) =
getimagesize($_FILES['new_image']['tmp_name']);//Get img size
        if ($new_image_width > $new_image_height) {//Get thumb size
        $percent_image_size = (200 / $new_image_width);
        }
        elseif ($new_image_width < $new_image_height) {
        $percent_image_size = (200 / $new_image_height);
        }
        else {
        $percent_image_size = (200 / $new_image_height);
        }
        $new_thumb_width = ($percent_image_size * $new_image_width);
        $new_thumb_height = ($percent_image_size * $new_image_height);

$new_thumb_file = imagecreatetruecolor($new_image_width, $new_image_height);
imagepng($new_image,
'/home/blacknut/public_html/picserv/picserv_mysql/pictures/' .
strtolower($_SESSION['user_sn']) . '/' . $new_pic_filename . '.png');
//Copy pic to user dir as pic_id.png


        $new_thumb_image = imagecreatetruecolor($new_thumb_width,
$new_thumb_height); // Create Thumb file
        imagecopyresampled($new_thumb_image, $new_image, 0, 0, 0, 0,
$new_thumb_width, $new_thumb_height, $new_image_width,
$new_image_height); //Copy resized to new thumb file
        imagepng($new_thumb_image,
'/home/blacknut/public_html/picserv/picserv_mysql/pictures/' .
strtolower($_SESSION['user_sn']) . '/' . $new_pic_filename . 't.png');
//Save image thumb

I am very new to file/image manipulation in php so if you see any
errors, or unnecessary code please tell me.  I'm not quit sure how
everything works, although I do have a theoretical picture of how it
does.  Any comments will be appreciated.

--nathan

--
http://www.blacknute.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
--- Begin Message ---
Ah, thank you.  after checking that the folder was there I found I had
made a mistake in the path to the folder. 8_ I can believe I didnt
find it before.  Aside from that error, did I go about the image
resizing and everything in the correct way?  What is the use of
imagecreatetruecolor().  Thanks for the help.

--nathan


On Sun, 30 Jan 2005 00:52:06 -0800, hitek <[EMAIL PROTECTED]> wrote:
> Are you making sure the $_SESSION['user_sn'] folder exists before saving
> the image into it?
> I'm asking because I originally thought it was a permission problem but php
> will tell you 'permission denied' if that was the case.
> 
> 
> At 05:05 PM 1/29/2005, NathanielGuy#21 wrote:
> >Hello everyone,
> >I have been troubleshooting a problem with one of my scripts for a
> >while now, its purpose is to allow a user to upload a file, save it to
> >the server, resize it into a thumbnail, and save the thumbnail as
> >well.  In my script all goes well until it comes to saving the images,
> >the script throws these errors:
> >
> >Warning: imagepng(): Unable to open
> >'/home/blacknut/public_html/picserv/picserv_mysql/pictures/blacknute/15.png'
> >for writing in /home/blacknut/public_html/picserv_mysql/addpic.php on
> >line 91
> >
> >Warning: imagepng(): Unable to open
> >'/home/blacknut/public_html/picserv/picserv_mysql/pictures/blacknute/15t.png'
> >for writing in /home/blacknut/public_html/picserv_mysql/addpic.php on
> >line 95
> >Picture "ffq" was added to our database! The following is what we have
> >generated and it is what others will see
> >
> >Here is the important part of the script, if more is needed i can post
> >it as well.
> >
> >         if ($mime_type == 'jpeg') {
> >         $new_image = imagecreatefromjpeg($_FILES['new_image']['tmp_name']);
> >//Create an image refrence
> >         }
> >         elseif ($mime_type == 'png') {
> >         $new_image = imagecreatefrompng($_FILES['new_image']['tmp_name']);
> >//Create an image refrence
> >         }
> >         elseif ($mime_type == 'gif') {
> >         $new_image = imagecreatefromgif($_FILES['new_image']['tmp_name']);
> >//Create an image refrence
> >         }
> >
> >   //Image Sizes
> >         list($new_image_width, $new_image_height) =
> >getimagesize($_FILES['new_image']['tmp_name']);//Get img size
> >         if ($new_image_width > $new_image_height) {//Get thumb size
> >         $percent_image_size = (200 / $new_image_width);
> >         }
> >         elseif ($new_image_width < $new_image_height) {
> >         $percent_image_size = (200 / $new_image_height);
> >         }
> >         else {
> >         $percent_image_size = (200 / $new_image_height);
> >         }
> >         $new_thumb_width = ($percent_image_size * $new_image_width);
> >         $new_thumb_height = ($percent_image_size * $new_image_height);
> >
> >         $new_thumb_file = imagecreatetruecolor($new_image_width,
> > $new_image_height);
> >   imagepng($new_image,
> >'/home/blacknut/public_html/picserv/picserv_mysql/pictures/' .
> >strtolower($_SESSION['user_sn']) . '/' . $new_pic_filename . '.png');
> >//Copy pic to user dir as pic_id.png
> >
> >         $new_thumb_image = imagecreatetruecolor($new_thumb_width,
> >$new_thumb_height); // Create Thumb file
> >         imagecopyresampled($new_thumb_image, $new_image, 0, 0, 0, 0,
> >$new_thumb_width, $new_thumb_height, $new_image_width,
> >$new_image_height); //Copy resized to new thumb file
> >         imagepng($new_thumb_image,
> >'/home/blacknut/public_html/picserv/picserv_mysql/pictures/' .
> >strtolower($_SESSION['user_sn']) . '/' . $new_pic_filename . 't.png');
> >//Save image thumb
> >
> >I am very new to file/image manipulation in php so if you see any
> >errors, or unnecessary code please tell me.  I'm not quit sure how
> >everything works, although I do have a theoretical picture of how it
> >does.  Any comments will be appreciated.
> >
> >--nathan
> >
> >--
> >http://www.blacknute.com/
> >
> >--
> >PHP General Mailing List (http://www.php.net/)
> >To unsubscribe, visit: http://www.php.net/unsub.php
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 


-- 
http://www.blacknute.com/

--- End Message ---
--- Begin Message --- NathanielGuy#21 wrote:
Ah, thank you.  after checking that the folder was there I found I had
made a mistake in the path to the folder. 8_ I can believe I didnt
find it before.  Aside from that error, did I go about the image
resizing and everything in the correct way?  What is the use of
imagecreatetruecolor().  Thanks for the help.

imagecreatetruecolor

(PHP 4 >= 4.0.6, PHP 5)
imagecreatetruecolor -- Create a new true color image
Description
resource imagecreatetruecolor ( int x_size, int y_size )

imagecreatetruecolor() returns an image identifier representing a black image 
of size x_size by y_size.

Example 1. Creating a new GD image stream and outputting an image.
<?php
header ("Content-type: image/png");
$im = @imagecreatetruecolor(50, 100)
     or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

    Note: This function requires GD 2.0.1 or later.

    Note: This function will not work with GIF file formats.

See also imagedestroy() and imagecreate().

---

for a more well laided out copy of this text goto:
http://nl2.php.net/imagecreatetruecolor

or maybe the manual wasn't clear to you and you are seeking further
clarification?



--- End Message ---
--- Begin Message ---
I have a script called index.php on my main page and it actually is used
to launch several webpages like so;

http://mysite.com/index.php?id=16

When http://mysite.com runs it still uses index.php. but it doesn't
show it in the address bar. All it shows is;

http://mysite.com

What I would like to do is to get the information from the address bar
to know when I am on the main page.

-- 

--- End Message ---
--- Begin Message ---
$file = basename(_FILE_);

On Sun, 2005-01-30 at 01:26, David Banning wrote:
> I have a script called index.php on my main page and it actually is used
> to launch several webpages like so;
> 
> http://mysite.com/index.php?id=16
> 
> When http://mysite.com runs it still uses index.php. but it doesn't
> show it in the address bar. All it shows is;
> 
> http://mysite.com
> 
> What I would like to do is to get the information from the address bar
> to know when I am on the main page.
> 
> -- 
-- 
Regards,
Matthew Fonda

--- End Message ---
--- Begin Message ---
Hello,
i've a general question concerning PHP's architecture.

Why isn't there a native packaging concept in PHP?

I think php became much more powerfull with the extended OOP features
introduced in PHP5 - without a packaging concept you couldn't use these
features "in big business" (in terms of sharing classes/libraries). I know
the discussion of OOP features vs. PHPs scripting capabilities, but IMHO
doing the half way of OOP isn't right...

Are there any thoughts about that in future releases?
What do other developers think about this issue?
How do organize multiple used classnames?

Thanks for your answers..
with best regards,

vivian

--- End Message ---
--- Begin Message ---
>From: "Vivian Steller" <[EMAIL PROTECTED]>

> i've a general question concerning PHP's architecture.
>
> Why isn't there a native packaging concept in PHP?

This was also suggested on comp.lang.php, recently (the "nested class"
thread), called "namespaces", but there wasn't a lot of enthusiasm for it.
Apparently, it was even implemented at one point, but then subsequently
dropped. I've found it difficult to find the relevant discussion in the
archive (possible the internals-list or Zend's engin2-list), could anyone
provide a link?

> I think php became much more powerfull with the extended OOP features
> introduced in PHP5 - without a packaging concept you couldn't use these
> features "in big business" (in terms of sharing classes/libraries). I know
> the discussion of OOP features vs. PHPs scripting capabilities, but IMHO
> doing the half way of OOP isn't right...

I guess package/namespace doesn't have a lot to do with OO (except that they
both allow grouping of functionality, and avoiding name collision, but
that's not the only benefit of OO), but as there hasn't been any enthusiasm
for overloading, either (not even for user-defined types, where you _can_
use "type hints" in function signatures), and it's common in OO languages, I
guess you have a point. Interestingly, I found that Perl has the possibility
of function overloading (also a language that's dynamically typed)
(http://www.math.tu-berlin.de/polymake/perl/overload.html) It also has -
like Python - operator overloading. But is there any enthusiasm for that in
the PHP community, either? Nah...

> Are there any thoughts about that in future releases?
> What do other developers think about this issue?
> How do organize multiple used classnames?

The common answer is: "Use a prefix"...

Regards,

Terje

--- End Message ---
--- Begin Message ---
<verÃffentlicht & per Mail versendet>

Thanks for your answer!

Terje Slettebà wrote:

>>From: "Vivian Steller" <[EMAIL PROTECTED]>
> 
>> i've a general question concerning PHP's architecture.
>>
>> Why isn't there a native packaging concept in PHP?
> 
> This was also suggested on comp.lang.php, recently (the "nested class"
> thread), called "namespaces", but there wasn't a lot of enthusiasm for it.
> Apparently, it was even implemented at one point, but then subsequently
> dropped. I've found it difficult to find the relevant discussion in the
> archive (possible the internals-list or Zend's engin2-list), could anyone
> provide a link?
> 
>> I think php became much more powerfull with the extended OOP features
>> introduced in PHP5 - without a packaging concept you couldn't use these
>> features "in big business" (in terms of sharing classes/libraries). I
>> know the discussion of OOP features vs. PHPs scripting capabilities, but
>> IMHO doing the half way of OOP isn't right...
> 
> I guess package/namespace doesn't have a lot to do with OO (except that
> they both allow grouping of functionality, and avoiding name collision,
> but that's not the only benefit of OO), but as there hasn't been any
> enthusiasm for overloading, either (not even for user-defined types, where
> you _can_ use "type hints" in function signatures), and it's common in OO
> languages, I guess you have a point. 
Thanks for metioning this issue! This is another point where I think OO is
done the half way in PHP:

Why do we need some implicit type check, like
        public method(Type $type)
if we then loose the optional parameter advantage?

Either some polymorphism mechanism should be implemented, or something like
        public method(Type $type = new MyType())
should be able being passed through the interpreter..

also the primitive datatypes should be (exceptionally) used here:
        public method(string $argument)

Otherwise (IMHO) the typecheck mechanism is useless.

> Interestingly, I found that Perl has 
> the possibility of function overloading (also a language that's
> dynamically typed)
> (http://www.math.tu-berlin.de/polymake/perl/overload.html) It also has -
> like Python - operator overloading. But is there any enthusiasm for that
> in the PHP community, either? Nah...
regrettably... :(

> 
>> Are there any thoughts about that in future releases?
>> What do other developers think about this issue?
>> How do organize multiple used classnames?
> 
> The common answer is: "Use a prefix"...
but i really dislike using cryptic and long prefixes to ensure uniqueness...
dots in the classname (ok, doing it like java) would be so nice, gr**

> 
> Regards,
> 
> Terje

vivian

--- End Message ---
--- Begin Message --- robert mena wrote:
Hi All,

I am looking for advice regarding design patterns/code generation in PHP5.

I have a simple code generation tool (written in PHP) to interface with
database.  It works fine for simple situations but seems a little
"strange" for more complex ones.

Suppose I have a table user (id, name, age) and a table
account(idAccount, idUser, name).  My generator creates a class for each
table and a standard db class.  Each class basically has a set/get
method for each property and some "standard" insert, delete, update,
search methods

ex.
class user
{
   var $db
   function setName(..)
   function getName()
   function insert()
   {
      $this->db->query("insert into user values...")
   }
...
}

I've omited the other methods/constructor but you can get the picture.

The problem comes when I have to access information the comes from 2+
tables.  Suppose I have to show all users accounts.   Now I end up with
something like this.

$u = new User() ;
$a = new Account() ;
if($u->search())
{
   for($i=0;$i<...)
   {
      $idUser = $u->getId() ;  $u->next() ;
       $a->setIdUser($idUser) ;
       if($a->search())
   {
      // print
   }
}

While this works I feel there must be an easier/cleaner way. If I was just
querying the database a join would give me the result in one pass.

the join is based on a foreign key constraint - An idea might be to generate a method based on the FK details (in either or both relevant classes), if you are using an older version of mysql, or don't actually define the constraint in the DB, then this maybe impossible - unless you use a config file to generate each class.

so that you can do:

$user = new User();
$ac = $user->getAccount(); // returns the correct Account object.



In my case what should I do ? create a new method "showAccounts" ? Where should I put it, in User or Account class ?

on the User class, Account would probably want a method like showUser().


I am interested in advices regarding how to design it better so I can make the proper adjustments in my generator.


--- End Message ---
--- Begin Message --- John, Hugh,
I'm not sure what you mean when you say "use PHP's FTP". I'm using $HTTP_POST_FILES because the files are retrieved through a web form.
As for the user, I would assume that it's whatever default for any viewer coming to a web page. I have people log in using a user name and password retrieved from a MySQL database, but I don't see how the browser or the server would know about that.
The thing is I'm really a newbie at this Unix server and file permission thing. I was really hoping that there was some parameter I could set to have the uploaded file set to full access permissions, as it took all the PHP scripting knowledge I had just to get it working as much as it is now.
I guess what confuses me most is, if the file was uploaded from a user from the web, how is it not already on the same permission level for other users also accessing the file from the web? It seems like any user can upload and make a file that they can't touch again, and requires an administrator to get rid of.


--
Dave Gutteridge
[EMAIL PROTECTED]

--- End Message ---
--- Begin Message ---
<?

 class A
 {
  var $name;
  function A($str)
  {
   $this->name = $str;
  }
 }

 $arr = array();

//Put to array to objects of class A,
// where their attribute A::a is assigned a different value
//objects are assigned to an array by reference

$a = &new A("qaz");
 $arr[0] = &$a;

 $a = &new A("wsx");
 $arr[1] = &$a;


//But watch the output!!!
// It is "(qaz)(qaz)", which means that the attribute of a first
// object assigned to array is outputted!!! WHY?!?!!!
 foreach($arr as $a)
 {
  echo "(".$a->name.")";
 }
?>

--- End Message ---
--- Begin Message ---
No

news.php.net wrote:
<?

 class A
 {
  var $name;
  function A($str)
  {
   $this->name = $str;
  }
 }

 $arr = array();

//Put to array to objects of class A,
// where their attribute A::a is assigned a different value
//objects are assigned to an array by reference

$a = &new A("qaz");
 $arr[0] = &$a;

$arr[0] and $a reference now the same variable


$a = &new A("wsx");

by changing $a you also changed $arr[0]

$arr[1] = &$a;

now $a, $arr[0] and $arr[1] reference the same variable, they just have different names.


More:
http://sk.php.net/manual/en/language.variables.php
http://sk.php.net/manual/en/language.references.php



//But watch the output!!!
// It is "(qaz)(qaz)", which means that the attribute of a first
// object assigned to array is outputted!!! WHY?!?!!!
 foreach($arr as $a)
 {
  echo "(".$a->name.")";
 }
?>


--- End Message ---
--- Begin Message ---
> No
>
> news.php.net wrote:
>> <?
>>
>>  class A
>>  {
>>   var $name;
>>   function A($str)
>>   {
>>    $this->name = $str;
>>   }
>>  }
>>
>>  $arr = array();
>>
>> //Put to array to objects of class A,
>> // where their attribute A::a is assigned a different value
>> //objects are assigned to an array by reference
>>
>> $a = &new A("qaz");
>>  $arr[0] = &$a;
>
> $arr[0] and $a reference now the same variable
>
>>
>>  $a = &new A("wsx");
>
> by changing $a you also changed $arr[0]

No. referencs are not pointers! Here in "  $a = &new A("wsx") " I assign $a
by reference, and now $a references the different location, and $arr[0]
references the same. Assigning $a by references doesnt affect $arr[0],
though the pointed to the same location before...
After all this is the prove:

This is my  previous assignment code:


 class A
 {
  var $name;
  function A($str)
  {
   $this->name = $str;
  }
 }

 $arr = array();

//Put to array to objects of class A,
// where their attribute A::a is assigned a different value
//objects are assigned to an array by reference

$a = &new A("qaz");
 $arr[0] = &$a;

 $a = &new A("wsx");
 $arr[1] = &$a;

//HERE IS THE PROVE
print_r($arr);
//Watch the OUTPUT... there are DIFFERENT objects laying in the array! This
is the point of my question...

// But if you do this:

 foreach($arr as $a)
 {
  echo "(".$a->name.")";
 }

// You see that iterating the objects it somehow references the same
object...
// or the same 'name' attrribute of the first object in the array...











>
>>  $arr[1] = &$a;
>
> now $a, $arr[0] and $arr[1] reference the same variable, they just have 
> different names.
>
> More:
> http://sk.php.net/manual/en/language.variables.php
> http://sk.php.net/manual/en/language.references.php
>
>>
>>
>> //But watch the output!!!
>> // It is "(qaz)(qaz)", which means that the attribute of a first
>> // object assigned to array is outputted!!! WHY?!?!!!
>>  foreach($arr as $a)
>>  {
>>   echo "(".$a->name.")";
>>  }
>> ?>
>>

--- End Message ---
--- Begin Message ---
I did a little experimenting, and it looks like foreach is misbehaving,
but may I just don't get it, anyway check this out
(my php version and output are below):

class A { var $name; function A($str) { $this->name = $str; } }

// does not work as expected.
$arr = array();
$a = &new A("qaz");
$arr[0] = &$a;      unset($a);
$a = &new A("wsx"); print_r($a);
$arr[1] = &$a;      print_r($arr[1]);
print_r($arr);
foreach($arr as $a) { echo "(".$a->name.")"; }
echo "\n"; //    ^-- I thought this might be the problem, hence:
foreach($arr as $b) { echo "(".$b->name.")"; }
echo "\n--\n";

// works as expected
$arrB = array();
$arrB[0] = &new A("qaz");
$arrB[1] = &new A("wsx");
print_r($arrB);
foreach($arrB as $c) { echo "(".$c->name.")"; }
echo "\n--\n";

// works as expected
$arrC = array();
$f = new A("qaz");
$arrC[0] = $f;
$f = new A("wsx");
$arrC[1] = $f;
print_r($arrC);
foreach($arrC as $d) { echo "(".$d->name.")"; }
echo "\n--\n";

// works as expected
$arrD = array();
$h =& new A("qaz");
$arrD[0] = $h;
$h =& new A("wsx");
$arrD[1] = $h;
print_r($arrD);
foreach($arrD as $g) { echo "(".$g->name.")"; }
echo "\n--\n";

// both items in the array reference the
// second object (references $i) - this is correct
$arrE = array();
$i = new A("qaz");
$arrE[0] =& $i;
$i = new A("wsx");
$arrE[1] =& $i;
print_r($arrE);
foreach($arrE as $j) { echo "(".$j->name.")"; }
echo "\n--\n";



---
which gives the following output on
PHP 5.0.2 (cli) (built: Nov  9 2004 19:00:36):


A Object ( [name] => wsx ) A Object ( [name] => wsx ) Array ( [0] => A Object ( [name] => qaz )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(qaz)
(qaz)(qaz)
--
Array
(
    [0] => A Object
        (
            [name] => qaz
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(wsx)
--
Array
(
    [0] => A Object
        (
            [name] => qaz
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(wsx)
--
Array
(
    [0] => A Object
        (
            [name] => qaz
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(wsx)
--
Array
(
    [0] => A Object
        (
            [name] => wsx
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(wsx)(wsx)
--


news.php.net wrote:
No


--- End Message ---
--- Begin Message ---
Well, unless I'm misstaken, the '=& new' should only be used once with every
class, so your code doesn't really make sense. The reason is that you are
trying to set the A to the new a, not $a to the new a.. Now, A couldn't get
changed I suppose, so therefore, $arr[1] will be a reference to $a, which is
still a reference to A.. Something like that.. I can only guess it'll work
as expected ("(wsx)(wsx)") if you change =&new to =new..

-- 
// DvDmanDT
MSN: dvdmandt�hotmail.com
Mail: dvdmandt�telia.com
"News.Php.Net" <[EMAIL PROTECTED]> skrev i meddelandet
news:[EMAIL PROTECTED]
> <?
>
>  class A
>  {
>   var $name;
>   function A($str)
>   {
>    $this->name = $str;
>   }
>  }
>
>  $arr = array();
>
> //Put to array to objects of class A,
> // where their attribute A::a is assigned a different value
> //objects are assigned to an array by reference
>
> $a = &new A("qaz");
>  $arr[0] = &$a;
>
>  $a = &new A("wsx");
>  $arr[1] = &$a;
>
>
> //But watch the output!!!
> // It is "(qaz)(qaz)", which means that the attribute of a first
> // object assigned to array is outputted!!! WHY?!?!!!
>  foreach($arr as $a)
>  {
>   echo "(".$a->name.")";
>  }
> ?>

--- End Message ---

Reply via email to