0

I'm starting with OOP in PHP and there is something I'd like to do. Let me show an example:

classes.php

<?php
    class a {
        public function a() {
            echo 'a';
        }
    }

    class b {
        public function calla() {
            $x->a();
        }
    }
?>

index.php

<?php
    include('classes.php');
    $x = new a();
    $d = new b();
    $d->calla();
?>

Is this possible? I know I can do something like:

class b {
        public function calla($object) {
            $object->a();
        }
    }

and

$d->calla($x);

but is the first example possible to do?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262

2 Answers2

0

I think I understand what you're trying to ask but you're mixing up parameters visibility concept with object passing in class methods. So let's clean up...

In your classes.php file the method calla() of the class b as absolutely nothing to do with class a, unless $x itself is an instance of the class a.

In your index.php file you instantiate $x as class a, but it is a global variable, and the class b has no visibility on it. Moreover, your call to $d->calla() would return an error because you are accessing a private method from outside.

Your third example gets closer to the solution, passing the instance to the calla() method helps class b seing the instance of class a, I guess you want to edit your index.php file to:

<?php
include('class.php');
$x = new a();
$d = new b();
$d->calla($x);
?>

But this still won't work until you change the private methods to public ones.

Frhay
  • 424
  • 2
  • 16
  • I wrongly typed private, it was meant to be public. I understand that i can call **$d->calla()** and it would throw an error if the object $x didn't existed. The reason i asked is because i'm initiating and querying a mysql database through an class, and i want all other classes to be able to use that object($db in case) to query the database, without the need to create an object in each class or pass it through the function. –  Feb 15 '13 at 16:54
  • You can use global($x) inside your class methods to make $x visible in the inner scope then... :) – Frhay Feb 18 '13 at 07:45
0

So you want to use $x as global variable? Syntax is the same, object or not:

class b {
    public function calla() {
        $GLOBALS['x']->a();
    }
}

... or:

class b {
    public function calla() {
        global $x;
        $x->a();
    }
}

Of course, it's a horrible design that injects hard to track dependencies and will probably lead to unexpected side effects and head-scratching.

Additionally, a() is a poor method name for class a because it's the PHP 4 syntax for constructors. If you run this code:

<?php

class a {
    public function a() {
        echo 'a';
    }
}

class b {
    public function calla() {
        $GLOBALS['x']->a();
    }
}

$x = new a();
$d = new b();
$d->calla();

... you'll see that a() runs twice. Guess why?

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • The global was what i wanted. Thank you. Didn't knew also about the naming of the method, thanks for the tip. –  Feb 15 '13 at 16:55