简体   繁体   中英

Accessing variables and methods outside of class definitions

Suppose I have a php file along these lines:

<?php
function abc() {  }

$foo = 'bar';

class SomeClass {  }
?>

Is there anything special I have to do to use abc() and $foo inside SomeClass ? I'm thinking along the lines of using global in a function to access variables defined outside the function.

(I'm new to OOP in PHP)

functions outside any class are global an can be called from anywhere. The same with variables.. just remember to use the global for the variables...

eg

<?php
function abc() {  }

$foo = 'bar';

class SomeClass {  
 public function tada(){
     global $foo;

     abc();
     echo 'foo and '.$foo;
 }
}
?>

Simply pass your variable to object instance through constructor or pass it to method when it's needed and remember to DON'T USE GLOBAL! ANYWHERE!

Why global is to be avoided?

The point against global variables is that they couple code very tightly. Your entire codebase is dependent on a) the variable name $config and b) the existence of that variable. If you want to rename the variable (for whatever reason), you have to do so everywhere throughout your codebase. You can also not use any piece of code that depends on the variable independently of it anymore. https://stackoverflow.com/a/12446305/1238574

You can use abc() everywhere because in PHP functions are globally accessible.

<?php
function abc() {  }

$foo = 'bar';

class SomeClass {
    private $foo;
    public function __construct($foo) {
        $this->foo = $foo;
    }
    public function doSomething() {
        abc($this->foo);
    }
}

class OtherClass {
    public function doSomethingWithFoo($foo) {
        abc($foo);
    }
}

$obj = new SomeClass($foo); // voillea

// or

$obj2 = new OtherClass();
$obj2->doSomethingWithFoo($foo);

functions are defined at a global level ; so, you don't need to do anything to use them from a method of your class.

For more informations, see the function page in the manual , which states (quoting) :

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.


For your $foo variable, on the other hand, you have to use the global keyword inside each method/function in which you want to access it.

For more informations, don't hesitate to read the page about Variable scope , which should bring you interesting informations ;-)


Edit after the comment :

Each method/function regardless of it being defined within a class or not?

If a "function" is defined inside a class, it's not called a "function" anymore, even if it is still the function that is used : it is called a "method"

Methods can be used statically :

MyClass::myMethod();

Or Dynamically :

$obj = new MyClass();
$obj->myMethod();

Depending on whether they were defined as static or not.


As a sidenote : if you are new to OOP in PHP, you should definitly invest some time to read the Classes and Objects (PHP 5) section of the manual : it will explain much.

Both function and variables cane be called inside of classes if called before

<?php
    function abc() {
        $ba = 'Hello';
        return $ba;
    }

$bar = 'World';

class foo {

    public function __construct() {
        global $bar;
        $body = abc();
        $bod = $bar;

        echo $body.$bod;
    }
}

$foo = new foo();
$foo;
  <?php    
  class Foo {
  public static $my_static = 'foo';

   public function staticValue() 
    {
     return self::$my_static;
     }
  }
  print Foo::$my_static."\n";//Accessing variables outside of class definitions
  $foo = new Foo();

  print $foo->staticValue()."\n";
  ?>    

Try to create a new class that call $foo and set a variable like

class SomeClass {  
 public function tada(){
     $foo = new Class;
     $foo = $foo->getFoo;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM