简体   繁体   English

如何在另一个页面的php类中使用定义的变量值

[英]How can I use the value of variables defined inside a php class in another page

I'm fairly new using PHP classes, I have done some PHP using the procedural method for a while but never fully done proper object oriented programming on php. 我是使用PHP类的新手,一段时间以来我已经使用过程方法完成了一些PHP,但从未完全在php上完成正确的面向对象编程。 Which brings me to my question: 这使我想到了一个问题:

I have one file called classes.php where I intend to define all my php classes. 我有一个名为classes.php的文件,打算在其中定义所有php类。 Inside that file I have created a class as follows: 在该文件中,我创建了一个如下类:

require_once("connection.php");

class User
    {

        public function getUserInfo() {
            $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
            while($row = mysqli_fetch_assoc($userinfo)) {

                // USER DETAILS:
                $userid = $row['id'];
                $username = $row['username'];
                $userfirstname = $row['first_name'];
                $userlastname = $row['last_name'];
                $useremail = $row['email'];
                $useraddress1 = $row['address_line_1'];
                $useraddress2 = $row['address_line_2'];
                $userpostcode = $row['postcode'];
                $userphone = $row['phone'];
                $usermobile = $row['mobile'];
                $usercreated = $row['created'];
                $usermodified = $row['modified'];
                $useraccess = $row['access_level'];

            }
        }

    } // end of User

Now I want to use the values of the variables defined in that class in another page. 现在,我想在另一页中使用该类中定义的变量的值。 But how can I access those values? 但是我该如何访问这些值?

So far I'm trying this (unsuccessfully): 到目前为止,我正在尝试(失败):

<?php
include "php_includes/classes.php";
$user = new User();
?>

<div class="somediv">
    <?php echo $user->getUserInfo($username) ?>
</div>

I have also tried: 我也尝试过:

<?php
    include "php_includes/classes.php";
    $user = new User();
    ?>

    <div class="somediv">
        <?php echo $user->$username ?>
    </div>

Is there any way to echo those variables in another page so I can use the user information in, for example, profile pages? 有什么方法可以在另一个页面中回显这些变量,以便可以在个人资料页面中使用用户信息?

Thanks in advance. 提前致谢。

Before your function you need make PUBLIC VARIABLE like: 在执行功能之前,您需要使PUBLIC VARIABLE像这样:

public $userid; 

Then next in code in function you need to use: 然后,您需要使用的功能代码如下:

$this->userid = $row['id'];

EDIT 编辑

Your calss need to be: 您的成绩必须是:

class User
    {

        public $userid;
        public function getUserInfo() {
            $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
            while($row = mysqli_fetch_assoc($userinfo)) {

                // USER DETAILS:
                $this->$userid = $row['id'];


            }
        }

    } // end of User

And you also need include this class file in other php page when you want show data. 当您想显示数据时,还需要在其他php页面中包含此类文件。

you have to declare the variable as PUBLIC 您必须将变量声明为PUBLIC

class User
{
       public function getUserInfo() {
       $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
       while($row = mysqli_fetch_assoc($userinfo)) {

                        // USER DETAILS:
                        public $userid = $row['id'];

        }
        }
} 

For more information check here 欲了解更多信息,请点击这里

you should return the value from function. 您应该从函数返回值。

require_once("connection.php"); require_once( “connection.php”);

class User
    {

        public function getUserInfo() {
            $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
            $row = mysqli_fetch_assoc($userinfo);

            return row
        }

    } 

and now only have to show the detail of user 现在只需要显示用户的详细信息

<?php
  include "php_includes/classes.php";
  $user = new User();
 ?>
 <div class="somediv">
    <?php $user_detail=$user->getUserInfo(); ?>
    <label>name:</label><br>
    <?php echo $user_detail["first_name"]; ?>
 </div>

Hence you can check whether row is null or not and whole array can be returned by this function of class any where this class included in file. 因此,您可以检查row是否为空,并且该类的函数可以在文件中包含此类的任何位置返回整个数组。

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

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