从其他文件的类函数获取变量
我想从其他文件的类函数中获取变量。
像这样的事情:
file lang.php:
<?php
class lang
{
function get()
{
$dubs = "dubs";
?>
file print.php:
<?php
require("lang.php");
lang::get();
echo $dubs;
?>
但这没有返回任何结果......
I want to get variable from other file's class function.
Something like this :
file lang.php:
<?php
class lang
{
function get()
{
$dubs = "dubs";
?>
file print.php:
<?php
require("lang.php");
lang::get();
echo $dubs;
?>
But this returns nothing...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
变量 $dubs 只能在函数 get() 中使用,因为您在那里声明了它。如果你想在外面使用它,有两个选择:或者在函数中返回它的值(在末尾添加 return $dubs),然后执行类似 $a = lang->get(); 的操作,或者使其全局的,在类中的任何函数之外声明它。使用哪种方法取决于类和函数的提议;如果您要在此类的其他函数中使用该值,则将其设为全局。
The variable $dubs can only be used inside the function get(), because you declared it there. If you want to use it outside, there are two options: or return its value in the function (add return $dubs at the end), and the do something like $a = lang->get();, or make it global, declaring it outside any function in the class. Which method to use depends on the propouse of the class and the function; if you are going to use the value in other functions of this class, then make it global.
正确的方法是
The correct way would be
$dubs 仅在其创建的函数中可见。您还可以像调用静态方法一样调用该方法。您需要使用常规语法:
$dubs will only be visible within the function it was created. You are also calling the method as if it was a static one. You need to use the regular syntax:
正如 Luan Nico 建议:
或
或
或
As Luan Nico suggested:
OR
OR
OR