Accessing variables and methods outside of class definitions

后端 未结 6 1612
盖世英雄少女心
盖世英雄少女心 2020-12-05 11:02

Suppose I have a php file along these lines:


Is there anything sp

相关标签:
6条回答
  • 2020-12-05 11:17
      <?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";
      ?>    
    
    0 讨论(0)
  • 2020-12-05 11:18

    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);
    
    0 讨论(0)
  • 2020-12-05 11:24

    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.

    0 讨论(0)
  • 2020-12-05 11:25

    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;
     }
    }
    
    0 讨论(0)
  • 2020-12-05 11:33

    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...

    e.g

    <?php
    function abc() {  }
    
    $foo = 'bar';
    
    class SomeClass {  
     public function tada(){
         global $foo;
    
         abc();
         echo 'foo and '.$foo;
     }
    }
    ?>
    
    0 讨论(0)
  • 2020-12-05 11:36

    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;
    
    0 讨论(0)
提交回复
热议问题