Magic Constants Case Sensitive or case In-sensitive?

Experts,

I do not understand this atall.
Here:

It says:
"PHP Magic constants
PHP provides a large number of predefined constants to any script which it runs.

There are five magical constants that change depending on where they are used. For example, the value of LINE depends on the line that it’s used on in your script. These special constants are case-insensitive and are as follows −".

CLASS

The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared u[/u]. In PHP 4 its value is always lowercased.

METHOD

The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared u[/u].

Seems like contradiction to me. Firs they say case-insensitive. Then in their 2 examples they mention they are case-sensitive. What is going on ? I need some good explanation with a min of 2 good examples to understand any of this.

It’s not a contradiction because they’re being used in two totally different ways. The case insensitive use means you can call any of these in any way and they will work (I think the leading and trailing underscores are really the trigger…)

All of these would return the same thing

echo __LINE__;
echo __line__;
echo __Line__;
echo __LiNe__;
echo __lInE__;

Where as CLASS and METHOD can still be called in the same manner, but what it RETURNED is case sensitive. Take this example and run it - look at what’s returned for base and Derived

<?phpabstract class base {
        function __construct() {}

        function init() {
			echo 'Base class (__CLASS__): ' . __CLASS__ . '<br />';
			echo 'Base class (__class__): ' . __class__ . '<br />';
			echo 'Base method (__METHOD__): ' . __METHOD__ . '<br />';
			echo 'Base method (__method__): ' . __method__ . '<br />';
			echo '<br />';
        }
}

class Derived extends base {
        function Init2() {
			echo 'Derived class (__CLASS__): ' . __CLASS__ . '<br />';
			echo 'Derived class (__class__): ' . __CLASS__ . '<br />';
			echo 'Derived method (__METHOD__): ' . __METHOD__ . '<br />';
			echo 'Derived method (__method__): ' . __method__ . '<br />';
			echo '<br />';
        }

}

$a = new derived();

$a->init();
$a->init2();
Base class (__CLASS__): base
Base class (__class__): base
Base method (__METHOD__): base::init
Base method (__method__): base::init

Derived class (__CLASS__): Derived
Derived class (__class__): Derived
Derived method (__METHOD__): Derived::Init2
Derived method (__method__): Derived::Init2
1 Like

Ok, Dave.
I’m still a beginner and don’t know how CLASS works.
Nevermind.

Cheers!

I just showed you in the sample code…CLASS returns the name of the class currently running and METHOD shows the exact method within the class being executed.

May I suggest using something like http://phptester.net/ to test some of these code samples yourself? That way you can change them around and see how the output changes?

I meant, I do not know what CLASS is. I did read on it once but not a beginner tutorial and it did my head in.
As for that link, thanks for suggesting it. It will be handy. I’m noting it to experiement on it. :slight_smile:

I got my wires crossed with this subject and thread. It can be closed. The tutorial was talking about case sensitive in magic quote names but their values being case insensitive. I didn’t understand that and thought they were saying at one place the values are case sensitive and on another place values are case in-sensitive. But I had a hunch what they were saying and needed more experienced guys to confirm it.

Thank you all!

This thread can now be closed.

class and method refers to OOP. What you are doing for your “login” project, that’s procedural. When you start to use classes, methods, objects and other varies OOP related syntax like extends, implements, etc and inheritance. Then you start to actually write reusable code. Right now, you’re still writing in procedural and won’t even touch basis with an application that has pure OOP for a long time until you understand basic PHP. That’s when you’ll become intermediate and learn about different design patterns and different ideology.

1 Like

I know I will become intermediate once I’ve learnt pdo and got a hang of basic oop.
Thanks for your input. I agree to everything you said. That was valuable input!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.