Page 1 of 1

wat does the operator :: use for?

Posted: Wed Sep 09, 2009 11:06 am
by John.Mike
hey,fellows ,i got some difficults when i was having the class knowledge of php.
codes belows:
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ")\n";
} else {
echo "\$this is not defined.\n";
}
}
}

class B
{
function bar()
{
A::foo();
}
}

$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>


it's an example on php.net....i am wondering wat does the opereator "::" means by ? would anyone give me a hand?

Re: wat does the operator :: use for?

Posted: Wed Sep 09, 2009 11:19 am
by dsainteclaire
You really could easily have searched on the php.net to determine that what you're looking for is the scope resolution operator that lets you call methods of classes outside the class you're currently in, without inheriting the method. Here is the detail for what you're looking for.

http://us3.php.net/manual/en/keyword.pa ... otayim.php

It should provide enough examples to clearly explain how and when to use it. 8)

Re: wat does the operator :: use for?

Posted: Wed Sep 09, 2009 11:44 am
by John.Mike
got it,it seems that case variables needn't to be changed ,we can use the "A::example()"solution to run the function in the class defined before.tks.

Re: wat does the operator :: use for?

Posted: Wed Sep 09, 2009 1:44 pm
by Darhazer
You should not use A::example(), if example() is not declared a static method, or you are not in a class that extends A