Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About scope and OOP in PHP

I am having trouble understanding how to work with objects.

The specific code:

class first{
    class second{
        public function widgets(){
            $a_variable = $a_value;
        }
        #1
    }
    $second = new second;
    #2
}
#3
$first = new first;
  1. If I initialize $a_variable as $a_variable it is only available inside the function, correct?
  2. If I initialize $a_varialbe as $this->a_variable it is only available inside class second, correct?
  3. Can I initialize $a_variable as $first->second->a_variable? If so, How would I call it at #1, #2, and #3?
  4. Can I initialize $a_varialbe as $this->second->a_variable? If so, How would I call it at #1, #2, and #3?

As you can see I am simply confused as to how OOP works.


First of all, I want to express how much I appreciate all of the help. I have already learned more than enough to consider this question a smashing success.

That said, even if it is poorly formulated, psuedo-code and invalid syntax, this code DOES run.

class class_1{

    public function function_1(){

        require('class_2.php');

        public function function_2_callback(){

            //%%%%%%  How do I set a variable here and get the DATA...

        }

        $this->class_2 = new class_2("function_2_callback");
    }

}

$class_1 = new class_1;

//&&&&&&&&&& Get the DATA here?



/* CONTENTS OF class_2.php */

class class_2($callback){

    call_user_function($callback);

}

Even if we have to look at this as an exercise. Can someone tell me how I would first set (@ %%%%%%%)and then call a variable (@ &&&&&&&&) as shown?

like image 936
Samantha P Avatar asked Feb 19 '23 10:02

Samantha P


1 Answers

First off: What you have there doesn't work, you cannot declare a class inside a class the way you are doing (notwithstanding conditionally declaring a class inside a function, which you should not do).

Scope in PHP (including OOP) is very simple:

  1. variables have function scope
  2. object properties are accessible if you have a reference to the object
  3. the visibility of object properties can be restricted

The only real scope you have is function scope for variables:

$a = 'foo';

function bar() {
    $a = 'bar';
}

The two $as are entirely unrelated, in different scopes. As simple as that.

class Foo {

    public $a = 'foo';

    public function bar() {
        $this->a;  // foo

        $a = 'bar';
    }

}

$foo = new Foo;
$foo->a;  // foo

An object property has no scope, it has visibility. You can access it if you have the object in scope. Above, $foo is the object. It's in scope, its property a is public, therefore it can be accessed using $foo->a. Inside the class, the property is accessible via $this->a.

The $a = 'bar' is a local variable in the function and has nothing to do with $this->a. It is not accessible anywhere except inside the function. Refer to rule #1, function scope.

class Bar {

    protected $b = 'bar';

    public function baz() {
        $this->b;  // bar
    }

}

$bar = new Bar;
$bar->b;  // doesn't work

If the visibility is not public, the property (here b) is not accessible from outside the class itself. Inside the class you can access it using $this->b, but not from outside using $bar->b. It's not about scope, but visibility.

And that's pretty much the scope rules in PHP.

like image 193
deceze Avatar answered Feb 27 '23 14:02

deceze