Php 在函数中使用来自调用的局部变量

Php 在函数中使用来自调用的局部变量,php,Php,经过9个小时的努力,我终于求助于互联网。通过谷歌搜索,我似乎找不到任何相关的答案 我现在有一个叫做Test的类。测试只接受一个参数 <?php class test { private $varpassed; public function getVarpas() { return $this->varpassed; } Public function setVarpas($value

经过9个小时的努力,我终于求助于互联网。通过谷歌搜索,我似乎找不到任何相关的答案

我现在有一个叫做Test的类。测试只接受一个参数

<?php
    class test {
        private $varpassed;

        public function getVarpas() {
            return $this->varpassed;
        }

        Public function setVarpas($value) {
            $this->varpassed= $value;
        }

        public function stringGen(){

            $testvar = $this->varpassed;
            echo $testvar;
        }
    }
是否有其他方法将变量传递给stringGen方法?我试过使用:

self::$this->varpassed;

这也会抛出一个错误。

您必须执行类似的操作

$var = new test();
$var->setVarpas("Hello");
$var->stringGen(); // this will echo Hello

$此
在上课时使用。在类之外,您必须使用类对象。

您必须这样做

$var = new test();
$var->setVarpas("Hello");
$var->stringGen(); // this will echo Hello

$此
在上课时使用。在类之外,您必须使用类对象。

首先创建对象的实例(以便您可以在上下文中使用$this),例如:

$test = new test();
然后你可以打电话:

$test->setVarpas('Hello World!');
$test->stringGen();
现在您可以拨打:

$test->setVarpas('Hello World!');
$test->stringGen();

首先创建对象的实例(以便可以在上下文中使用$this),例如:

$test = new test();
然后你可以打电话:

$test->setVarpas('Hello World!');
$test->stringGen();
现在您可以拨打:

$test->setVarpas('Hello World!');
$test->stringGen();

不应使用括号声明类

使用
类测试{
而不是
class test(){

不应该用括号声明类

使用
类测试{
而不是
class test(){

1)将此更改:
class test()
更改为
class test

2)首先创建并实例类似于
$t1=newtest();

3)调用函数
$t1->setVarpas(5);

4)现在您可以调用函数
$t1->stringGen();

固定:

<?php
class test
{
private $varpassed;
public function getVarpas() {
return $this->varpassed;
}

Public function setVarpas($value) {
$this->varpassed= $value;
}

public function stringGen(){

$testvar = $this->varpassed;
echo $testvar;
}
}

$t1 = new test();
$t1->setVarpas(5);
$t1->stringGen();
5
1)将此更改:
class test()
更改为
class test

2)首先创建并实例类似于
$t1=newtest();

3)调用函数
$t1->setVarpas(5);

4)现在您可以调用函数
$t1->stringGen();

固定:

<?php
class test
{
private $varpassed;
public function getVarpas() {
return $this->varpassed;
}

Public function setVarpas($value) {
$this->varpassed= $value;
}

public function stringGen(){

$testvar = $this->varpassed;
echo $testvar;
}
}

$t1 = new test();
$t1->setVarpas(5);
$t1->stringGen();
5


类测试而不是类测试()
解析错误:语法错误,意外“(”,在第2行的F:\xampp\htdocs\testing\experiments\debug.php中应为“{”,将其重命名为
类测试
而不是类测试()
解析错误:语法错误,意外“(”,意外“{'在F:\xampp\htdocs\testing\experiments\debug.php的第2行
将其重命名为
class test
instead,我试图将变量传递给名为stringGen的方法,然后我希望在调用该方法时通过返回函数返回该变量。创建对象并调用该方法没有问题,但没有问题eem接受方法中的变量。谢谢,将此作为解决方案的一部分。我无法进行投票,但谢谢!我正在尝试将变量传递给名为stringGen的方法,然后我希望在调用该方法时通过返回函数返回变量。创建对象并调用该方法没有问题,但确实如此似乎不接受方法中的变量。谢谢,将此作为解决方案的一部分。我无法投票,但谢谢!$varpassed不是静态的。变量是使用SetVarPass方法动态分配的。不过修复了类。谢谢。@DairyB不客气!请保持安全并继续编码!
^
$varpassed不是静态的。变量iable是使用setVarpas方法动态分配的。修复了类。谢谢。@DairyB不客气!保持安全并继续编码!
^\u^
谢谢,修复了应用程序。仍然无法传递变量。谢谢,修复了应用程序。仍然无法传递变量。谢谢。这很有效。我调用的方式函数是这样的:test::stringGen();这是问题的根源。当我将其切换到$test->stringGen()时,它开始工作。再次感谢。@DairyB很高兴听到我可以帮助您:)如果您仍然想使用test::stringGen()您应该将属性和方法设置为静态。这样您也可以使用self::祝您愉快。谢谢。这很有效。我调用函数的方式是这样的:test::stringGen();这导致了问题。当我将其切换到$test->stringGen()时,它开始工作。再次感谢。@DairyB很高兴听到我可以帮助您:)如果您仍然想使用test::stringGen(),那么应该将属性和方法设置为静态。这样您也可以使用self::了。祝您度过愉快的一天。