PDA

View Full Version : [PHP] Calling method from different class



Gothic
12-20-2007, 07:37 AM
Hi, I'm kinda new to this object oriented thing in php.

My problem is when trying to call a method declared in class A from class B

Like this:



file: /inc/a.class.php
01 | <?
02 | class a{
03 | public function __contruct() {
04 | $this->msg = 'abc';
05 | }
06 | public function getMsg() {
07 | return $this->msg;
08 | }
09 | }
10 | ?>

file: /inc/b.class.php
01 | <?
02 | class b{
03 | public function __construct() {
04 | $this->msg = 'def';
05 | }
06 | public function showMsg() {
07 | echo a::getMsg;
08 | echo $this->msg;
09 | }
10 | }
11 | ?>

file: /index.php
01 | <?
02 | function __autoload($clname) {
03 | require '/inc/'.strtolower($clname).'.class.php';
04 | }
05 |
06 | $aClass = new a;
07 | $bClass = new b;
08 |
09 | $bClass->showMsg(); //error
10 |
11 | b::showMsg(); // error
12 |
13 | ?>


I keeps claiming: Using $this when not in object context.

And I don't think tha instancing each class inside every class is the smartest thing to do. No to mention that by doing such, I'll create a new object, wich is not my goal. But I'm just stuck here... any ideas would be really appreciated!

Thx!

Rips
12-21-2007, 10:12 AM
can u pass a pointer of type class A to class B

then call it: echo p_aClass->GetMsg() ?