Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46474 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 29238 invoked from network); 19 Dec 2009 10:20:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Dec 2009 10:20:34 -0000 X-Host-Fingerprint: 92.225.40.175 g225040175.adsl.alicedsl.de Received: from [92.225.40.175] ([92.225.40.175:20359] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 15/13-03420-1F8AC2B4 for ; Sat, 19 Dec 2009 05:20:33 -0500 Message-ID: <15.13.03420.1F8AC2B4@pb1.pair.com> To: internals@lists.php.net References: <83.E2.63064.C512C2B4@pb1.pair.com> <1261183971.2756.1171.camel@guybrush> Date: Sat, 19 Dec 2009 11:20:29 +0100 Lines: 40 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="utf-8"; reply-type=original Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5843 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Posted-By: 92.225.40.175 Subject: Re: [PHP-DEV] is_a() versus instanceof From: johannes_m@gmx.net ("Johannes Mueller") Johannes "Schlüter" wrote: > On Sat, 2009-12-19 at 01:42 +0100, Johannes Mueller wrote: >> if($foo instanceof bar){ >> .. >> } >> // runs without any notification > > instanceof is a language construct expecting a class identifier. > It doesn't complain about on-existing classes as it would need to > trigger the __autoloader which might be way too much. > >> and >> >> if(is_a($foo, bar)){ >> .. >> } >> // runs with an undefined constant bar notification > > is_a() is a function expecting a string. And this warning is unrelated > to the fact that the class "bar" doesn't exist, but due to the way PHP > treats undefined constants. Thank you for your answers, that is what I've expected. Unfortunately this is sometimes annoying when using namespaces, where you have to take care of the namespaces too. I do understand the performance impact of a more complex solution and my fault was to mix two different operations. I expected instanceof and is_a() to check if the class exists. My personal problem was, that I didn't recognized instanceof to treat the requested class as a normal string, even it is not enclosed by quotation marks. The only protective barrier in this case seems to be a skilled IDE, that helps you to avoid referencing non existant classes. Cheers Johannes