PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a main class in which i am calling different class files by using :: (scope resolution operator). In the other classes i.e the class files i am using $this-> operator to call the methods of main class as well as methods of other classes, i am able to access all their methods. (without using extend).
require_once('classA.php');
require_once('classB.php');
require_once('classC.php');
Class Main {
// methods of main class
function EchoError($error)
{
return "<b>$error</b>";
}
.
.
// methods of other classes called, so that i can call all the methods of any class using one class obj. I am using like a connector to other classes
function callAmethodFromClassA(param1)
{
return ClassA :: callAmethodFromClassA(param1);
}
function callAmethodFromClassB(param1)
{
return ClassB :: callAmethodFromClassB(param1);
}
// so on
}
In ClassA file
require_once('mainclass.php');
require_once('classB.php');
require_once('classC.php');
Class ClassA
{
function FirstMethodOfClassA()
{
$this->EchoError('this is error1');
}
function SecondMethodOfClassA()
{
$this->callAmethodFromClassB(param1);
}
}
in the same way classB and other class are written. I am not extending classes still i am able to access using $this operator
require_once('classA.php');
class Main
{
function CheckWhereIam($name)
{
return ClassA :: CheckWhereIam()
}
function CheckWhereIamMain()
{
echo 'I am in main class';
}
}
require_once('classMain.php');
class ClassA
{
function CheckWhereIam()
{
$this->CheckWhereIamMain();
}
}
class A
{
var $prop_of_a = "HELLO";
function foo_a() {
B :: foo_b();
}
}
class B
{
function foo_b() {
echo $this->prop_of_a;
}
}
$a = new A;
$a->foo_a(); // HELLO
You're wondering why B has access to the property of A via $this, although B doesn't extend A. The answer is, that php4 used to propagate "$this" across static calls, i.e. if in context of some method you're calling some other method statically ( :: ), the current value of $this is passed to that second method. This behavior is deprecated but still supported in php5. Note that php6 will completely disallow static calls to non-static methods and vice versa.