[PHP] unpack() - how to handle bitfields??

2002-12-28 Thread Tim Molendijk
Hi all,

I'm wrestling with this problem for a while now. I want to read a binary
file (created by C software) using PHP. I know of unpack(). But the C struct
that is stored in the binary file contains variables with bitfields:

struct some_struct {
char val_a : 2
int val_b : 7
}

Such a construct means that val_a is char with a bitfield of 2 and therefore
can contain values between 0 (00 binary) and 3 (11 binary). val_b is an
integer with a range of 0 (000) to 127 (111).

Now the problem is: how on earth do I handle those values with PHP's
unpack()?!? I've tried a lot of syntaxes but none of them seem to work. Can
someone please shortcut whether it is possible or not and if yes, *how*?

Thanks in advance,
Tim



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




[PHP] unpack() based on C struct

2002-12-26 Thread Tim Molendijk
Hi all,

Assume we've got a binary data file 'profile' which stores the data of a C
struct. The struct is as follows:

struct s_profile {
char id[32];
char vala:3;
char valb:1;
char valc:7;
}

Now I would like to read 'profile' using PHP's unpack(). So like this:
$f = fopen('profile', 'r');
$binaryData = fread($f, filesize('profile'));
$data = unpack('a32id/[...etc]', $binaryData);

My problem is: which formats should I use for char vala:3, char valb:1 &
char valc:7 ? In other words: what should be on the spot of [...etc] ?
cvala/cvalb/cvalc doesn't work.

Thanks,
Tim Molendijk



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




[PHP] Flushing image from database to browser - how to get it cached??

2002-12-22 Thread Tim Molendijk
Hi all,

I'm storing a bunch of images in a MySQL database. I use a small PHP script
to load the appropriate image from database and flush it to the browser.
Because this script flushes in content-type image/jpeg (or similar) it can
be used as IMG SRC. So images can be shown on a webpage using the following
HTML: 

This works perfectly fine but the browser (IE) does not cache these images.
On one hand I do understand why -- the image source is a script, not a
static image file, so the browser qualifies the resulting content as
dynamic. On the other hand though: in my setup image.php?id=1 is (as good
as) eternally the same image and for image.php?id=2, image.php?id=3, etc.
the same thing. So it would be no problem if the browser would cache f.e.
image.php?id=1 as if it is a static image. It would even be desirable
because it saves bandwidth.

So actually the issue is: how can I get browser to cache image.php?id=1 (...
etc.) just as it caches image1.jpg (... etc.)?? The only thing I've thought
of is using mod_rewrite to rewrite URI's like image1.jpg into image.php?id=1
but I haven't tried it yet -- don't know if it's a logical idea.

Thanks in advance,
Tim Molendijk



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




Re: [PHP] Function returning a reference - how ?!?

2002-12-21 Thread Tim Molendijk
Wow!! You're my king! Thanks, that syntax is completely new to me!

Great, another thing learned today! :)

Cheers mate!

<[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
>
> change your createParent function to return by reference:
>
> function &createParent()
> {
>return new Parent;
> }
>
> it returns 'two' and 'two' for me
>
> Regards,
> - reynard
>
> >Hi all,
> >
> >I came across the following weird situation. I don't get it, I hope some
of
> >you can explain the logic of it all.
> >
> >[begin code]---
> > >
> >/* Define class Parent. Its constructor sets an instance of Child in
> >property $child and passes itself to Child's constructor. */
> >class Parent
> >{
> >var $child;
> >var $property = 'one';
> >
> >function Parent()
> >{
> >$this->child = &new Child($this);
> >}
> >}
> >
> >/* Define class Child. Its constructor assigns a reference of the Parent
> >object that contains this Child object to property $parent. */
> >class Child
> >{
> >var $parent;
> >
> >function Child(&$p)
> >{
> >$this->parent = &$p;
> >}
> >}
> >
> >/* This function instantiates and returns a new Parent class. */
> >function createParent()
> >{
> >return new Parent;
> >}
> >
> >/* $p is a reference of the Parent object created by createParent(). */
> >$p = &createParent();
> >
> >/* The $property property of the Parent object is changed. */
> >$p->property = 'two';
> >
> >/* Since $p should be the same object as $p->child->parent, the following
> >should print twice the same. */
> >print $p->property . '';
> >print $p->child->parent->property . '';
> >
> >?>
> >[end code]---
> >
> >Now the problem is: IT DOES NOT PRINT TWICE THE SAME!!!
> >Instead it prints 'two' and then 'one', which indicates that
> >$p->child->parent is another object than $p. The problem is that the
object
> >*returned* by createParent() is not the same object as the object that is
> >*created* by createParent(). Just replace the line $p = &createParent();
by
> >$p = &new Parent(); -- this works perfectly fine! Conclusion:
createParent()
> >does not return the object it creates!
> >
> >Someone knows why this ain't working and how to solve it?!? Hope so,
can't
> >wait to hear from you guys.
> >
> >Thanks in advance,
> >
> >Tim Molendijk
>
>
> __
> The NEW Netscape 7.0 browser is now available. Upgrade now!
http://channels.netscape.com/ns/browsers/download.jsp
>
> Get your own FREE, personal Netscape Mail account today at
http://webmail.netscape.com/



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




[PHP] Function returning a reference - how ?!?

2002-12-21 Thread Tim Molendijk
Hi all,

I came across the following weird situation. I don't get it, I hope some of
you can explain the logic of it all.

[begin code]---
child = &new Child($this);
}
}

/* Define class Child. Its constructor assigns a reference of the Parent
object that contains this Child object to property $parent. */
class Child
{
var $parent;

function Child(&$p)
{
$this->parent = &$p;
}
}

/* This function instantiates and returns a new Parent class. */
function createParent()
{
return new Parent;
}

/* $p is a reference of the Parent object created by createParent(). */
$p = &createParent();

/* The $property property of the Parent object is changed. */
$p->property = 'two';

/* Since $p should be the same object as $p->child->parent, the following
should print twice the same. */
print $p->property . '';
print $p->child->parent->property . '';

?>
[end code]---

Now the problem is: IT DOES NOT PRINT TWICE THE SAME!!!
Instead it prints 'two' and then 'one', which indicates that
$p->child->parent is another object than $p. The problem is that the object
*returned* by createParent() is not the same object as the object that is
*created* by createParent(). Just replace the line $p = &createParent(); by
$p = &new Parent(); -- this works perfectly fine! Conclusion: createParent()
does not return the object it creates!

Someone knows why this ain't working and how to solve it?!? Hope so, can't
wait to hear from you guys.

Thanks in advance,

Tim Molendijk



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




[PHP] Use PHP for reading binary files created by C software

2002-11-25 Thread Tim Molendijk
Hi all,

I would like to use PHP to read a binary file which contains a structure in
C, such as:
struct simple {
int a;
float b;
};
with f.e. a = 2 and b = 1.5.
Please notice that this is stored as *binary* data and not as text.

I know it is possible to read binary files using fgets() in PHP 4.3.0 or
higher but then I still don't have a clue how to actually *do* it...
Is it even possible to read binary files created by C software and if so,
how to do this in practice - examples would be very welcome.

Thanks in advance,
Tim



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




Re: [PHP] Weird behaviour with references to objects...

2002-11-09 Thread Tim Molendijk

"Ernest E Vogelsinger" <[EMAIL PROTECTED]> wrote in message
news:5.1.1.6.2.20021108175627.00b44880@;mail.vogelsinger.at...
>
> This is clearly said in your PHP.ini file:
>
> ; Whether to enable the ability to force arguments to be passed by
reference
> ; at function call time.  This method is deprecated and is likely to be
> ; unsupported in future versions of PHP/Zend.  The encouraged method of
> ; specifying which arguments should be passed by reference is in the
function
> ; declaration.  You're encouraged to try and turn this option Off and make
> ; sure your scripts work properly with it in order to ensure they will
work
> ; with future versions of the language (you will receive a warning each
time
> ; you use this feature, and the argument will be passed by value instead
of by
> ; reference).
> allow_call_time_pass_reference = On
>
> ...god has spoken :)

yup that's one hell of an argument :)

>
> I'd rather have the abilty to pass A reference like "foo(&$a)" and have
the
> opportunity to choose at runtime if I want to use a reference here or not,
> but it's how they say...
>
>
> --
>>O Ernest E. Vogelsinger
>(\) ICQ #13394035
> ^ http://www.vogelsinger.at/
>



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




Re: [PHP] Weird behaviour with references to objects...

2002-11-08 Thread Tim Molendijk

"Ernest E Vogelsinger" <[EMAIL PROTECTED]> wrote in message
news:5.1.1.6.2.20021108170633.00b8cb60@;mail.vogelsinger.at...
>
> You might always simply omit the parameter when calling the function
> (you'll get a notice about that if enabled). You just cannot pass
constants:
>
> function Product(&$language)
> {
>...
> }
>
> // ok, but gives a notice
> Product();
>
> // will fail
> Product('ger');
>

ok well that's an idea indeed... i'll think about it.

> If you keep your code like it is be warned that it may break with a future
> version of PHP.
>

why do you think it won't be supported by future versions? all i do is
passing an object by reference. because in this case this function always
should accept its argument as a reference and therefore is bad programming
habit does not mean the php construction is bad or weird!?!

>
> --
>>O Ernest E. Vogelsinger
>(\) ICQ #13394035
> ^ http://www.vogelsinger.at/
>



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




Re: [PHP] Weird behaviour with references to objects...

2002-11-08 Thread Tim Molendijk

"Ernest E Vogelsinger" <[EMAIL PROTECTED]> wrote in message
news:5.1.1.6.2.20021108165218.03238948@;mail.vogelsinger.at...
> One more:
>
> True for now, but this is "officially deprecated". Functions or methods
> accepting a reference are required to be "prototyped" that way - this
helps
> you not to "forget" to pass by reference. You really should modify your
> functions using reference parameters when declaring them, if you're using
> references...
>

Yes I know it is poor programming habits to do like I did... But I have a
reason for it. I have quite a lot methods that accept objects *optional*. So
f.e.:
---
class Product
{
var $language;
function Product($language = FALSE)
{
$this->language = $language;
}
}
---
Now to make this possible it becomes impossible to do what you suggest:
function Product(&$language = FALSE)
is not allowed. And in my application this optional arguments functionality
is very important. So I have preferred this over the decent habit.

>
> --
>>O Ernest E. Vogelsinger
>(\) ICQ #13394035
> ^ http://www.vogelsinger.at/
>



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




Re: [PHP] Weird behaviour with references to objects...

2002-11-08 Thread Tim Molendijk
"Ernest E Vogelsinger" <[EMAIL PROTECTED]> wrote in message
news:5.1.1.6.2.20021108092657.02befe20@;mail.vogelsinger.at...
> At 03:36 08.11.2002, Tim Molendijk said:
> [snip]
>
> Ahh - culprit 1:
>
> You said the container is creating a child instance - I assume it's done
in
> the constructor...
>
> What you're doing here is to create a clone when assigning new()'s result
> to $testContainer1. You should rather
> $testContainer1 =& new Container(TRUE);
>
> Note the ampersand here - if you don't you'll get a clone.
>
> Rule for references:
> If your object does something in its constructor, use a reference
> assignment for
> new class()
>

Ernest,

Before your post I accidently found out a solution:
$testContainer1 = &new Container(TRUE) instead of $testContainer1 = new
Container(TRUE);

When I found this out I didn't understand WHY this was the solution...

Thanks to your comments I DO understand why... I suddenly see every jigsaw
piece fall in place :D

The difference between the first code snippet (that with $testContainer1)
and the second code snippet (that with $testContainer2) is that in code
snippet 2 the Container object is already constructed and assigned to
$testContainer2 when loading it. Then when it is loaded $this (in add())
refers to the object in $testContainer2 so no problem occurs.
But in code snippet 1 the Container object loads itself before it is
assigned to $testContainer1. During this process $this (in add()) refers to
the object that is being created. And now the core is: the object that is
being created is *not* the same object as the one that is assigned to
$testContainer1!! So the references to the created object will not refer to
$testContainer1!! That's it!!! I completely understand... Thanks a lot
Ernest!

>
> Again - make this
> $newChild =& new Child(1);
>
>
> But you don't pass "$child" by reference! Make this
> function (&$child)
>
> If you don't pass $child as a reference, the line
> $child->parent = $this;
> will set the parent value of the clone that's passed as an argument, and
> leave the "original" $child unmodified!
>

This is not true in my case, because instead of doing:
function add(&$child) { ... }
I do:
function add($child) { ... }
and then call it with:
$this->add(&$child);

This has the same result... As far as I know I doesn't matter whether you
put an ampersand before the variable in your function definition or your in
your function call.

> Crosscheck all of your class code (and the code utilizing it) it you're
> always passing references. Ref's work like a charm until you mess up at a
> single location. Well, having made my way from C/C++ to PHP I'd rather
deal
> with pointers, but unfortunately PHP doesn't have them. So keep an eye
open .-)
>

The problem is solved and I completely understand why now. This last thing
is thanks to you Ernest.

Regards,
Tim

>
> --
>>O Ernest E. Vogelsinger
>(\)ICQ #13394035
> ^ http://www.vogelsinger.at/
>
>



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




Re: [PHP] Weird behaviour with references to objects...

2002-11-07 Thread Tim Molendijk
Hi all,

First of all, thank you for the suggestions.

Actually I accidently found a solution to the problem... Not that I
understand it now, I just found a way to cope with it. By changing the first
line of the first code snippet:
$testContainer1 = new Container(TRUE);
into:
$testContainer1 = &new Container(TRUE);
the problem is gone!

Don't ask me WHY this is the solution. In my Container->add() method I
assign A REFERENCE of the Container object (&$this) to $child->parent so I
don't see why creating the Container object by reference is useful...

If anyone has additional information on this subject feel free to post!

Thanks everyone!
Tim.

"Mike Mannakee" <[EMAIL PROTECTED]> wrote in message
news:20021108044622.68446.qmail@;pb1.pair.com...
> This sounds like a matter that ought to make it to the programmers who
code
> PHP itself.  In the meantime can you code around it now that you know the
> bug is there?
>
> Mike
>
>
> "Marco Tabini" <[EMAIL PROTECTED]> wrote in message
> news:1036725595.21741.868.camel@;localhost.localdomain...
> > I'm not sure if my answer is going to be of much help, but I think this
> > has to do with the way PHP handles references and copies of objects--it
> > uses what I understand is a lazy copy mechanism, and thus the results on
> > object manipulations can yield odd results. There was a discussion on a
> > similar problem on PHP-DEV a while back--if you search the forums you
> > should be able to find it.
> >
> > Hope this helps...
> >
> >
> > Marco
> > -----
> > php|architect -- The Monthly Magazine For PHP Professionals
> > Come visit us on the web at http://www.phparch.com!
> >
> > On Thu, 2002-11-07 at 21:36, Tim Molendijk wrote:
> > > First of all I would like to say that I know this is a lot of text but
I
> > > would be very pleased if you take a little time to read it
nevertheless.
> The
> > > situation described below is not complicated at all...
> > > =
> > >
> > > Hello all,
> > >
> > > I'm dealing with this really weird problem which occurs when dealing
> with
> > > references within objects pointing to other objects. I'm trying to
find
> out
> > > what it is now for a few days and I still don't get it. I minimised
the
> code
> > > around the problem and it's weirder than ever. I would really
appreciate
> it
> > > when you could take a look... I'm pretty experienced at PHP but
perhaps
> you
> > > know something that I don't know.
> > >
> > > We have two different classes: Container and Child. Container objects
> > > contain a Child object in its $child property. A Child object has a
> $parent
> > > property, which contains a *reference* to the Container object by
which
> it
> > > is held.
> > > So $containerObject should be the *same* as
> $containerObject->child->parent.
> > > To test this Container objects have a $property property. When
changing
> this
> > > property the difference between one object and identical/cloned
objects
> > > becomes visible.
> > >
> > > Now take a look at the following code. The indented text is code, the
> rest
> > > are comments. The code for the class Container and class Child are
also
> > > included. Please scroll down or take a look at the attached file.
> > >
> > > begin first code snippet 
> > >  > >
> > > /* New Container object created. At the same time it sets a Child
object
> in
> > > its $child attribute. (This is indicated by passing TRUE.) */
> > > $testContainer1 = new Container(TRUE);
> > > /* The Container object is created, the Child object is set and its
> > > reference to the Container object is set. Now it prints
> > > $testContainer1->property and $testContainer->child->parent->property.
> These
> > > should have an identical value because they should be from the same
> object
> > > (not cloned/identical, but the same).
> > > This is the result:
> > > property = 'state 1'
> > > child->parent->property = 'state 1'
> > > So this is as expected, no problem... yet. */
> > > $testContainer1->printState();
> > >
> > > /* Now $testContainer1's $property property is changed into another
> value.
> > > /*
> > > $testContainer1->prope

[PHP] Weird behaviour with references to objects...

2002-11-07 Thread Tim Molendijk
First of all I would like to say that I know this is a lot of text but I
would be very pleased if you take a little time to read it nevertheless. The
situation described below is not complicated at all...
=

Hello all,

I'm dealing with this really weird problem which occurs when dealing with
references within objects pointing to other objects. I'm trying to find out
what it is now for a few days and I still don't get it. I minimised the code
around the problem and it's weirder than ever. I would really appreciate it
when you could take a look... I'm pretty experienced at PHP but perhaps you
know something that I don't know.

We have two different classes: Container and Child. Container objects
contain a Child object in its $child property. A Child object has a $parent
property, which contains a *reference* to the Container object by which it
is held.
So $containerObject should be the *same* as $containerObject->child->parent.
To test this Container objects have a $property property. When changing this
property the difference between one object and identical/cloned objects
becomes visible.

Now take a look at the following code. The indented text is code, the rest
are comments. The code for the class Container and class Child are also
included. Please scroll down or take a look at the attached file.

begin first code snippet 
property and $testContainer->child->parent->property. These
should have an identical value because they should be from the same object
(not cloned/identical, but the same).
This is the result:
property = 'state 1'
child->parent->property = 'state 1'
So this is as expected, no problem... yet. */
$testContainer1->printState();

/* Now $testContainer1's $property property is changed into another value.
/*
$testContainer1->property = 'state 2';
/* Now $testContainer1->child->parent->property should also have value
'state 2', but it HAS NOT!
This is the result:
property = 'state 2'
child->parent->property = 'state 1'
Obviously $testContainer1->child->parent->property is not the same object as
$testContainer1, but a clone! */
$testContainer1->printState();

?>
end first code snippet 

Well, this is the whole problem... Why on earth isn't it a reference, while
I really did assign it as a reference in the code (see class code).

Now to make the whole thing even weirder, take a short look at the following
code. It is almost completely the same as above, except that instead of
giving the Container constructor the command to assign a Child object to the
$child property, this command is given manually by calling method load() --
which is the method that Container constructor uses to load the Child
object.

begin second code snippet 
load();
$testContainer2->printState();

$testContainer2->property = 'state 2';
/* After $property has been modified again, $testContainer2's state is
printed again, and now the result is DIFFERENT!!!:
property = 'loaded state 2'
child->parent->property = 'loaded state 2'
This is the CORRECT result!!! This is what we also expected from the first
code snippet!!! And this while the executed codes are in fact identical!!!
How on earth is this possible?!? */
$testContainer2->printState();

?>
end second code snippet 

Well, this is the contradiction I wanted to show you all. I don't see the
logics of it all, but ofcourse I could be missing one obvious thing...
actually I hope so, because I need the construction as in the first code
snippet.

Below the code for the classes Container en Child.

begin third code snippet 
property = 'not loaded';
if ($load) {
$this->load();
}
}

function load()
{
$newChild = new Child(1);
$this->add(&$newChild);
$this->property = 'state 1';
}

function add($child)
{
/* Here a reference of $this (the Container object) is assigned to $child's
$parent attribute. */
$child->parent = &$this;
/* Here $child (the newly created Child object) is assigned to the
container's $child property. */
$this->child = &$child;
}

function printState()
{
print 'property = \'' . $this->property . '\'' .
'child->parent->property = \'' . $this->child->parent->property
. '\'';
}

}

class Child
{

var $id;
var $parent;

function Child($id)
{
$this->id = $id;
}

}

?>
end third code snippet 

Well that's it. I hope someone can help me on this. I'm not a person who
asks for help very quick. I prefer finding it out myself, also because I'm
pretty experienced at PHP. But this is the first time I really can't figure
out what it is... I rea

[PHP] Difference between "$object = new Class" and "$object =& new Class"

2002-10-15 Thread Tim Molendijk

Hi,

I'm using a 3rd pary debug class (ErrorHandler -
http://www.phpclasses.org/browse.html/package/345.html) in this project I'm
working on. For this ErrorHandler class to work it just needs to be
instantiated at the beginning of a php file and it will catch the errors,
warnings and notices.

But I found out that this only functions when instantiating like this:
$errorH =& new ErrorHandler;
and it doesn't work when you do this:
$errorH = new ErrorHandler;

I find this very awkward because:
- I don't see the use of a reference when creating a new object.
- I don't see why it does NOT work when assigning the object while it DOES
work when assigning a reference to the object.

Does anyone have a clue why this could be happening?? I'm _very_ curious coz
I simply don't get it. 8[

Greetz,
Tim



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