简体   繁体   English

如何在 function 内部的 class 外部传递变量?

[英]How do I pass a variable outside a class inside a function?

I´am learning PHP and have to deal with a tiny problem, which I already figured out what it is.我正在学习 PHP 并且必须处理一个小问题,我已经弄清楚它是什么。

The error: " Notice: Undefined variable: Notice: Undefined variable: connection in C:\xampp\htdocs\Aufgabe\config.php on line 38".错误:“注意:未定义的变量:注意:未定义的变量:C:\xampp\htdocs\Aufgabe\config.php 在第 38 行的连接”。

The error appears, because $connection is´nt definded inside of the class.出现错误,因为在 class 内部没有定义 $connection。 Acually I want to pass the methods the $connection variable as parameters, but couldnt figure out how.实际上,我想将 $connection 变量作为参数传递方法,但不知道如何。

<?php 
require('connection.php');
$instance = new database();
$connection = $instance->connection();

class article{
    
    public function poll(){
        $test = isset($_GET['page']) ? $_GET['page'] : '';
        if ($test =='add_article')
        {
            $this->add_articles();
        }
        elseif ($test=='my_articles')
        {
            $this->my_articles();
        }
            
        else{
            
        }
        return;
    }
    
    public function my_articles(){
        include ("my_articles.php");
        

    }
    public function add_articles(){
        require("add_article.php");
        
        $username = isset($_POST['username']) ? $_POST['username'] : '';
        $amount = isset($_POST['amount']) ? $_POST['amount'] : '';
        $comment = isset($_POST['comment']) ? $_POST['comment'] : '';


        $stmt = $connection->prepare("INSERT INTO items(item_name, amount, comment) VALUES(:username, :amount, :comment)");
        $stmt->bindParam(":username",$username);
        $stmt->bindParam(":amount",$amount);
        $stmt->bindParam(":comment",$comment);
        $stmt->execute();   
    }     
}

$call = new article();
$call->poll();

?>

either pass it to the function as parameter or use a member variable of your class article: eg:要么将其传递给 function 作为参数,要么使用 class 文章的成员变量:例如:

<?php 
require('connection.php');
$instance = new database();
$connection = $instance->connection();

class article{
private $mConnection;

public function poll(){
    $test = isset($_GET['page']) ? $_GET['page'] : '';
    if ($test =='add_article')
    {
        $this->add_articles();
    }
    elseif ($test=='my_articles')
    {
        $this->my_articles();
    }
        
    else{
        
    }
    return;
}

public function my_articles(){
    include ("my_articles.php");
}

public function add_articles($connection){
    $connection = $this->getConnection();
    require("add_article.php");
    
    $username = isset($_POST['username']) ? $_POST['username'] : '';
    $amount = isset($_POST['amount']) ? $_POST['amount'] : '';
    $comment = isset($_POST['comment']) ? $_POST['comment'] : '';


    $stmt = $connection->prepare("INSERT INTO items(item_name, amount, comment) VALUES(:username, :amount, :comment)");
    $stmt->bindParam(":username",$username);
    $stmt->bindParam(":amount",$amount);
    $stmt->bindParam(":comment",$comment);
    $stmt->execute();   
}

public function setConnection($mConnection) {
    $this->mConnection = $mConnection;
}

public function getConnection() {
    return $this->mConnection;
}
}

$call = new article();
$call->setConnection($connection);
$call->poll();

?>

edit: to pass as parameter:编辑:作为参数传递:

$call->poll($connection);
...
public function poll($connection){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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