php 访问类定义之外的变量和方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1415577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:28:43  来源:igfitidea点击:

Accessing variables and methods outside of class definitions

php

提问by Alex S

Suppose I have a php file along these lines:

假设我有一个这样的 php 文件:

<?php
function abc() {  }

$foo = 'bar';

class SomeClass {  }
?>

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

有什么特别的我必须使用abc()$foo内部SomeClass吗?我正在考虑global在函数中使用来访问函数外定义的变量。

(I'm new to OOP in PHP)

(我是 PHP 中 OOP 的新手)

回答by Vidar Vestnes

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;
 }
}
?>

回答by brzuchal

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 globalis to be avoided?

为什么global要避免?

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

反对全局变量的一点是它们非常紧密地耦合代码。您的整个代码库取决于 a) 变量名称 $config 和 b) 该变量的存在。如果您想重命名变量(无论出于何种原因),您必须在整个代码库中的任何地方都这样做。您也不能再使用任何依赖于变量的代码。https://stackoverflow.com/a/12446305/1238574

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

您可以abc()在任何地方使用,因为在 PHP 中函数是全局可访问的。

<?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);

回答by Pascal MARTIN

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.

PHP 中的所有函数和类都具有全局作用域 - 即使它们是在函数内部定义的,它们也可以在函数外部调用,反之亦然。


For your $foovariable, on the other hand, you have to use the globalkeyword inside each method/function in which you want to access it.


$foo另一方面, 对于您的变量,您必须global在要访问它的每个方法/函数中使用关键字。

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

有关更多信息,请随时阅读有关Variable scope的页面,它应该会给您带来有趣的信息 ;-)


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 functionthat is used : it is called a "method"

如果在一个类中定义了一个“函数”,它就不再被称为“函数”,即使它仍然function被使用:它被称为“方法”

Methods can be used statically :

方法可以静态使用:

MyClass::myMethod();

Or Dynamically :

或动态:

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

Depending on whether they were defined as staticor 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.


作为旁注:如果您不熟悉 PHP 中的 OOP,您肯定应该花一些时间阅读手册的类和对象(PHP 5)部分:它会解释很多。

回答by Kevin Roy

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;

回答by Sakthi Sakthivel

  <?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";
  ?>    

回答by user1933057

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

尝试创建一个调用 $foo 的新类并设置一个变量,如

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