php-general Digest 22 May 2011 14:31:08 -0000 Issue 7323

Topics (messages 313061 through 313071):

Re: A Review Request
        313061 by: Robert Cummings

Re: Closing Session (Revisited)
        313062 by: Roger Riordan

Re: Check the byte sequence  of a file to tell if it is  UTF-8 without the BOM 
using PHP ?
        313063 by: Eli Orr (Office)
        313064 by: Peter Lind
        313065 by: Eli Orr (Office)
        313066 by: Peter Lind
        313067 by: Eli Orr (Office)

Queries and Common Practices
        313068 by: admin.buskirkgraphics.com
        313069 by: Ashley Sheridan
        313070 by: Richard Quadling

context when calling non static method of class in a static way
        313071 by: Simon Hilz

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On 11-05-21 09:26 AM, tedd wrote:
At 2:49 PM -0400 5/19/11, Joshua Kehn wrote:
On May 19, 2011, at 2:44 PM, Andre Polykanine wrote:

  Hello Alex,

  Two (stupid?) questions:
  1. Why PHP_SELF is better than SCRIPT_NAME?
  2. Why strcmp() is better than just comparing?

  --
  With best regards from Ukraine,
  Andre
  Skype: Francophile
  My blog: http://oire.org/menelion (mostly in Russian)
  Twitter: http://twitter.com/m_elensule
  Facebook: http://facebook.com/menelion

No idea about the first, and I've never used strcmp() before for an
equality check. If there is something I'm missing I would love to
know.

Regards,

-Josh

-Josh:

The function strcmp() simply evaluates two strings and reports back
-1, 0, or 1 depending upon their  alphabetical relationship.

And therein lies it's advantage over a a direct comparison. This feature makes it great for sorting callbacks since you can just return the result of strcmp().

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--- End Message ---
--- Begin Message ---
On Thu, 05 May 2011 08:28:53 -0400, sstap...@mnsi.net (Steve Staples) wrote:

>On Thu, 2011-05-05 at 21:41 +1000, Roger Riordan wrote:
>> 
>> I have developed a common engine which I use for several different websites. 
>> I had been
>> using PHP 5.2.? and IE6 (yes; I know!), and had been able to have multiple 
>> sessions open
>> at once, displaying the same or different websites, without them interfering 
>> with each
>> other. This was incredibly useful; I could be looking at, or even edit, 
>> different parts of
>> the same, or different, websites simultaneously without any problems.
>> 
>> But I recently had a hard disk crash and had to re-install all the system 
>> software. Now I
>> have PHP 5.3 and IE 8, and find that if I try to do this the various 
>> sessions interfere
>> with each other. From the above comment I gather that this is because IE 8 
>> combines all
>> the instances, whereas previously each instance was treated as a different 
>> user.
>> 
>> Is there any simple way to make IE 8 treat each instance as a new user, or 
>> should I switch
>> to Chrome and use the Incognito feature?
>> 
>> Roger Riordan AM
>> http://www.corybas.com/
>> 
>
>The Incognito feature wont give you the results you're looking for.
>>From my experience, the incognito window(s) and tab(s) share the same
>memory/cookie/session space, which is different from the main window...
>which means you will run into the same issue.
>
>Once you close all your incognito windows/tabs, you will release those
>cookies/sessions/memory space and if you open a new one afterwards, then
>you will be fine, but if one tabs stays open, no go :(
>
>Have you looked at the http://ca3.php.net/session_name function, and
>putting that into your site just after your session_start() ?  I believe
>that will fix your issues (as long as your session names are unique),
>but i am not 100% sure.
>
>Steve
>

Thank you for this suggestion. This has solved the more serious half of my 
problems; I can
easily generate a different session name for each website, so that the various 
websites
don't interfere with each other, but I have not been able to devise a way to 
differentiate
between multiple sessions of the same website.

For example, if I open one copy of a website as a visitor I am shown as 
Visitor, but if I
then open another window, and log in as Manager, then go back to the first 
window I am
shown as Manager (with appropriate privileges) there also.

The only way I can think of to overcome this would be to generate a new named 
session
every time I log in, and then to pass the session name as a parameter every 
time I load a
new page. Unfortunately my program is sufficiently complicated that this is 
effectively
impractical, as it would involve tracking down and modifying every point in the 
program at
which a new page can be launched.

It also has a theoretical disadvantage that if someone bookmarks a page they 
will book
mark the session name, but this can fairly readily be overcome.

Is there any alternative way in which a different session name (or equivalent 
flag) can be
attached to each instance of the browser?

(Effectively these problems only affect the developer, as they only apply to 
multiple
instances of the same browser on the same PC.)


PS. At this stage I devised a really nasty kludge, which enables me to run 
multiple copies
without them interfering. In my program new pages are always launched by a 
command of the
general type:

http://localhost/cypalda.com/index.php?level=1&item=22

This loads the file index.php, which is a very brief file in the public 
directory
(cypalda.com in this case). It sets a couple of constants and then transfers 
control to a
file Begin.php, in a private directory. This in turn sets up a whole lot more 
constants,
and then transfers control to the main program, which is common to 5 different 
websites.

I realised that if I specify the session name in index.php, I can make several 
copies of
this file, e.g. index.php, index1.php, index2.php, each of which specified a 
different
session name. I thought this still left me the problem of modifying all the 
points at
which a new page was launched, but then I found that by great good fortune (or 
foresight!)
I had defined a constant $home_page = index.php, and always launched a new page 
with the
basic command
echo ('<a href="'.$home_page.'?ident=' ....... >');

So all I had to do to achieve the desired outcome was to specify a different 
$homepage in
each copy of index.php. Then, once I had launched a particular copy of 
index.php, that
instance of the browser would always load the session appropriate to that copy.

Even better, if I upload the various versions of index.php, I can run multiple 
copies of
the public website on the same PC without them interfering.


Roger Riordan AM
http://www.corybas.com/

--- End Message ---
--- Begin Message ---
Hi Adam,

I have a prof that the XML advise does not work in real cases I had.
We are using XMLs in our system but when you edit the XML with a text editor and put the XML heading of UTF-8
<?xml version="1.0" encoding="UTF-8"?>

it DOES NOT assure the text inside is encoded in UTF-8 so but maybe (many cases) t other iso-xxx method.

My question was for a function that scan the bytes of the file and decided WITHOUT the BOM heading.
I mean by checking the bytes sequence in the file.

I claim that WITHOUT a BOM it might be impossible to assure it is UTF-8 encoding which is a whole escape sequence logic
that may convert one character into one, two or three character.

Any advise if I'm right on this or smart file scan function that makes it?

Eli
On 21/05/2011 20:03, Adam Richardson wrote:
On Sat, May 21, 2011 at 12:10 PM, Eli Orr (Office) <eli....@logodial.com <mailto:eli....@logodial.com>> wrote:


    Dear PHP Gurus,

    I have a debate on the following please let me know what is true /
    false.

    I'am using a PHP function *is_UTF_8_file ($file_name) *that I've
    found as part of my PHP 5.3 installation.
    This function checks if the file start with the 3 UTF-8 BOM bytes.

    However another guy told me that there is way to detect if a file
    is a UTF-8 without having the BOM at the file start.
    To me it sounds impossible since if you do not have this
    indication you have a stream of bytes that you can never tell 100%
    if that is UTF-8 or else.

    Who is rigt here ?
    If there is a Magical function that can detect files without a BOM
    if they are UTF-8 or not please share you knowledge if this
    is not a "NULL" or impossible function as I thought.


Here's a great write-up I've got bookmarked (he points out Windows Notepad automatically determines the encoding):
http://codesnipers.com/?q=node/68

    * If it's an XML file, the structure allows you determine the
      encoding.
    * For other files, you can encode it as UTF-8 and look for
      improper encodings.


As far as a PHP function that already does this, I'm not aware of it, but you could make a system call to "file" if your on Linux, as it tries to automatically determine the encoding:
http://linux.die.net/man/1/file

Adam

--
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com


--
Best Regards,

*Eli Orr*
CTO & Founder
*LogoDial Ltd.*
M:+972-54-7379604
O:+972-74-703-2034
F: +972-77-3379604

Plaut 10, Rehovot, Israel
Email: _Eli.Orr@LogoDial.com_
Skype: _eliorr.com_

--- End Message ---
--- Begin Message ---
On 22 May 2011 08:17, Eli Orr (Office) <eli....@logodial.com> wrote:
> Hi Adam,
>
> I have a prof that the XML advise does not work in real cases I had.
> We are using XMLs in our system but when you edit the XML with  a text
> editor and put the XML heading of UTF-8
> <?xml version="1.0" encoding="UTF-8"?>
>
> it DOES NOT assure the text inside is encoded in UTF-8 so but maybe (many
> cases) t other iso-xxx method.

The point of the header is telling readers what encoding is used. Of
course that means errors are possible - setting the header is not
magic, it doesn't change the rest of the file. You need to make sure
the contents of the file match the encoding from the header when you
make XML documents.

Anyway, from your perspective, the header is an indication but not a
foolproof way of figuring encoding out.

> My question was for a function that scan the bytes of the file and decided
> WITHOUT the BOM heading.
> I mean by checking the bytes sequence in the file.
>
> I claim that WITHOUT a BOM it might be impossible to assure it is UTF-8
> encoding which is a whole escape sequence logic
> that may convert one character into one, two or three character.

http://se.php.net/manual/en/function.mb-detect-encoding.php - the
first comment should be interesting to you.

*****
If you try to use mb_detect_encoding to detect whether a string is
valid UTF-8, use the strict mode, it is pretty worthless otherwise.

<?php
    $str = 'áéóú'; // ISO-8859-1
    mb_detect_encoding($str, 'UTF-8'); // 'UTF-8'
    mb_detect_encoding($str, 'UTF-8', true); // false
?>
****

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>

--- End Message ---
--- Begin Message ---
Dear Peter,

But my point was different.

If you DO NOT have any BOM of a File does

mb_detect_encodin


can detect the file type by scanning the whole file ??

Thanks

Eli

On 22/05/2011 09:53, Peter Lind wrote:
On 22 May 2011 08:17, Eli Orr (Office)<eli....@logodial.com>  wrote:
Hi Adam,

I have a prof that the XML advise does not work in real cases I had.
We are using XMLs in our system but when you edit the XML with  a text
editor and put the XML heading of UTF-8
<?xml version="1.0" encoding="UTF-8"?>

it DOES NOT assure the text inside is encoded in UTF-8 so but maybe (many
cases) t other iso-xxx method.
The point of the header is telling readers what encoding is used. Of
course that means errors are possible - setting the header is not
magic, it doesn't change the rest of the file. You need to make sure
the contents of the file match the encoding from the header when you
make XML documents.

Anyway, from your perspective, the header is an indication but not a
foolproof way of figuring encoding out.

My question was for a function that scan the bytes of the file and decided
WITHOUT the BOM heading.
I mean by checking the bytes sequence in the file.

I claim that WITHOUT a BOM it might be impossible to assure it is UTF-8
encoding which is a whole escape sequence logic
that may convert one character into one, two or three character.
http://se.php.net/manual/en/function.mb-detect-encoding.php - the
first comment should be interesting to you.

*****
If you try to use mb_detect_encoding to detect whether a string is
valid UTF-8, use the strict mode, it is pretty worthless otherwise.

<?php
     $str = 'áéóú'; // ISO-8859-1
     mb_detect_encoding($str, 'UTF-8'); // 'UTF-8'
     mb_detect_encoding($str, 'UTF-8', true); // false
?>
****

Regards
Peter



--
Best Regards,

*Eli Orr*
CTO & Founder
*LogoDial Ltd.*
M:+972-54-7379604
O:+972-74-703-2034
F: +972-77-3379604

Plaut 10, Rehovot, Israel
Email: _Eli.Orr@LogoDial.com_
Skype: _eliorr.com_

--- End Message ---
--- Begin Message ---
On 22 May 2011 09:03, Eli Orr (Office) <eli....@logodial.com> wrote:
> Dear Peter,
>
> But my point was different.
>
> If you DO NOT have any BOM of a File does
>
> mb_detect_encodin
>
> can detect the file type by scanning the whole file ??
>

A few points:
1. top-posting on this list is frowned upon. Please bottom-post.
2. I did not write anything about BOM as far as I can recall. Neither
does the page I linked to contain much about BOM (I really suggest
reading it - as pointed out, the first comment should help you)

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>

--- End Message ---
--- Begin Message ---
Thank you Peter.

Can you please advise if

mb_detect_encodin does detect the file type by its structure / content?

Thanks

Eli

On 22/05/2011 10:12, Peter Lind wrote:
On 22 May 2011 09:03, Eli Orr (Office)<eli....@logodial.com>  wrote:
Dear Peter,

But my point was different.

If you DO NOT have any BOM of a File does

mb_detect_encodin

can detect the file type by scanning the whole file ??

A few points:
1. top-posting on this list is frowned upon. Please bottom-post.
2. I did not write anything about BOM as far as I can recall. Neither
does the page I linked to contain much about BOM (I really suggest
reading it - as pointed out, the first comment should help you)

Regards
Peter



--
Best Regards,

*Eli Orr*
CTO & Founder
*LogoDial Ltd.*
M:+972-54-7379604
O:+972-74-703-2034
F: +972-77-3379604

Plaut 10, Rehovot, Israel
Email: _Eli.Orr@LogoDial.com_
Skype: _eliorr.com_

--- End Message ---
--- Begin Message ---
I have been working on a class methods for some time now.

 

I have reached a cross road when it comes to common practice of developing
query structure.

 

Long ago I wrote queries where I  just called the field I wanted on a
particular table unless I was joining them.

 

Example:

$query = " SELECT id FROM Table WHERE Clause";   

 

Through time I developed a habit of queering as such.

Example:

$query = "SELECT tablename.id FROM db.table WHERE clause";

 

 

I have felt that, because my server contains multiple databases and I needed
to jump between databases and tables without changing the connector this
always has been best practice for me.

 

Someone recently told me,

                Rich, 

I do not agree with your design of the queries.

There is no need to include the DB and table name in the query if you are
not joining tables.

 

 

While I have a very hard time understanding this response as being valid. I
will propose the question. 

 

 

Is it bad practice to write queries with the database and table name in the
queries even if I am NOT joining tables?

Is there an impact from PHP or MySQL that is caused by doing so?

 

I know this more a MySQL question but as PHP developers we all deal with
queries on a day to day bases, 

and when developing more flexible class methods I build the queries in the
method.

 

 

Richard L. Buskirk


--- End Message ---
--- Begin Message ---
On Sun, 2011-05-22 at 05:33 -0400, ad...@buskirkgraphics.com wrote:

> I have been working on a class methods for some time now.
> 
>  
> 
> I have reached a cross road when it comes to common practice of developing
> query structure.
> 
>  
> 
> Long ago I wrote queries where I  just called the field I wanted on a
> particular table unless I was joining them.
> 
>  
> 
> Example:
> 
> $query = " SELECT id FROM Table WHERE Clause";   
> 
>  
> 
> Through time I developed a habit of queering as such.
> 
> Example:
> 
> $query = "SELECT tablename.id FROM db.table WHERE clause";
> 
>  
> 
> 
> 
> I have felt that, because my server contains multiple databases and I needed
> to jump between databases and tables without changing the connector this
> always has been best practice for me.
> 
>  
> 
> Someone recently told me,
> 
>                 Rich, 
> 
> I do not agree with your design of the queries.
> 
> There is no need to include the DB and table name in the query if you are
> not joining tables.
> 
>  
> 
> 
> 
> While I have a very hard time understanding this response as being valid. I
> will propose the question. 
> 
>  
> 
> 
> 
> Is it bad practice to write queries with the database and table name in the
> queries even if I am NOT joining tables?
> 
> Is there an impact from PHP or MySQL that is caused by doing so?
> 
>  
> 
> I know this more a MySQL question but as PHP developers we all deal with
> queries on a day to day bases, 
> 
> and when developing more flexible class methods I build the queries in the
> method.
> 
>  
> 
> 
> 
> Richard L. Buskirk
> 


I don't know of any impact, but I don't use the database name in the
query much, only when I need to perform a join across different
databases. However, I almost always specify the table name, even if I'm
doing a single table query, as I often find I may need to alter the
query to pull in extra information from other tables. I do it much like
you do, but I also give the table a moniker which lets me shorten the
queries as I type:

SELECT p.id, p.name FROM people p WHERE p.gender = 'male'

This way, I can easily join in other tables, my typing is kept to a
minimum as I do it also.

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
On 22 May 2011 10:50, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:
> On Sun, 2011-05-22 at 05:33 -0400, ad...@buskirkgraphics.com wrote:
>
>> I have been working on a class methods for some time now.
>>
>>
>>
>> I have reached a cross road when it comes to common practice of developing
>> query structure.
>>
>>
>>
>> Long ago I wrote queries where I  just called the field I wanted on a
>> particular table unless I was joining them.
>>
>>
>>
>> Example:
>>
>> $query = " SELECT id FROM Table WHERE Clause";
>>
>>
>>
>> Through time I developed a habit of queering as such.
>>
>> Example:
>>
>> $query = "SELECT tablename.id FROM db.table WHERE clause";
>>
>>
>>
>>
>>
>> I have felt that, because my server contains multiple databases and I needed
>> to jump between databases and tables without changing the connector this
>> always has been best practice for me.
>>
>>
>>
>> Someone recently told me,
>>
>>                 Rich,
>>
>> I do not agree with your design of the queries.
>>
>> There is no need to include the DB and table name in the query if you are
>> not joining tables.
>>
>>
>>
>>
>>
>> While I have a very hard time understanding this response as being valid. I
>> will propose the question.
>>
>>
>>
>>
>>
>> Is it bad practice to write queries with the database and table name in the
>> queries even if I am NOT joining tables?
>>
>> Is there an impact from PHP or MySQL that is caused by doing so?
>>
>>
>>
>> I know this more a MySQL question but as PHP developers we all deal with
>> queries on a day to day bases,
>>
>> and when developing more flexible class methods I build the queries in the
>> method.
>>
>>
>>
>>
>>
>> Richard L. Buskirk
>>
>
>
> I don't know of any impact, but I don't use the database name in the
> query much, only when I need to perform a join across different
> databases. However, I almost always specify the table name, even if I'm
> doing a single table query, as I often find I may need to alter the
> query to pull in extra information from other tables. I do it much like
> you do, but I also give the table a moniker which lets me shorten the
> queries as I type:
>
> SELECT p.id, p.name FROM people p WHERE p.gender = 'male'
>
> This way, I can easily join in other tables, my typing is kept to a
> minimum as I do it also.
>
> --
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

I use multiple databases on multiple servers (using linked servers).
In many cases the databases contain the same table names. It is a
force of habit that I use [server].[database].[owner].[table] alias.

If your connection is linked to a single DB and the query is only for
that DB, then [server].[database].[owner] is redundant.

Richard.

-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

--- End Message ---
--- Begin Message ---
hi,

lets assume the following classes:

class Foo{

public function bar()
        {
        echo get_class($this);
        }

}

class Foobar{

public function callBarStatic()
        {
        Foo::bar();
        }

}

the following code results in the output "Foobar":

$obj = new Foobar();
$obj->callBarStatic();

That means that the static call of bar() is executed in the context of Foobar. Is this behavior deliberate? If so, it would open a great way of object composition patterns. But only if it will be retained in future versions :) (i've tested with 5.3.5)


Simon Hilz

--- End Message ---

Reply via email to