在我的页面调用PHP类函数 [英] Calling PHP Class Function In My Page

查看:91
本文介绍了在我的页面调用PHP类函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类文件:我们将其称为 class.php 。其功能是从 ini 文件中抓取信息(是的,我发布了关于安全性的问题,并给出了使用配置文件或ini文件来保存数据库信息的重要建议) 。

基本上,我的课程是这样的:

 < php 
class myClass
{
public function getAttached()
{
$ file =../../myFile.ini;
if(!$ settings = parse_ini_file($ file,TRUE))抛出新异常('Unable to open'。$ file。'。');

$ hoost = $ settings ['mysqli'] ['default_host'];
$ useer = $ settings ['mysqli'] ['default_user'];
$ pazz = $ settings ['mysqli'] ['default_pw'];
$ dbs = $ settings ['mysqli'] ['default_db'];

$ con = mysqli_connect($ hoost,$ useer,$ pazz,$ dbs);

返回$ con;
}
}
$ obj = new myClass();
$ obj-> getAttached();
$ vals = $ obj-> getAttached();
// echo $ vals; //不知道我是否应该回应此问题。
?>

我想在我的 somePage.php 文件中调用它以使我的mysqli连接并从那里开始...



我试过这个:

  require_once( 'class.php'); 
getAttached();

很明显,这行不通(我知道它不会 - 但我反正只是为了看看是否可能),所以 - 如何从我的班级文件在常规的PHP页面调用该函数?



任何想法,将不胜感激。
在此先感谢。

解决方案

在调用函数之前,您需要创建一个类的实例,因为它们是不是静态的。

  require_once('class.php'); 
$ myClass = new myClass();
$ myClass-> getAttached();

或者像我上面所说的那样,您可以使该函数成为静态的。

  public static function myFunction(){
// etc ...
}

然后调用它可以使用:

  require_once ( 'class.php'); 
myClass :: getAttached();


I have a class file: we'll call it class.php. The functionality of that is to grab info from an ini file (yeah, I posted the question about security and was given the great suggestion to use either a config file or an ini file to hold the DB information).

Essentially, my class is this:

<?php  
class myClass
{   
    public function getAttached()
    {
        $file = "../../myFile.ini";
        if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.');

        $hoost = $settings['mysqli']['default_host'];
        $useer = $settings['mysqli']['default_user'];
        $pazz = $settings['mysqli']['default_pw'];
        $dbs = $settings['mysqli']['default_db'];

        $con = mysqli_connect($hoost ,$useer, $pazz, $dbs);

        return $con;
    }
}
    $obj = new myClass();
    $obj->getAttached();
    $vals = $obj->getAttached();
    //echo $vals;  //didn't know if I should echo this or not.
?>

I want to call this in my somePage.php file to make my "mysqli" connection and go from there...

I tried this:

require_once('class.php');
getAttached();

Obviously that didn't work (I knew it wouldn't but - I did it anyway just to see if "maybe"), so - how do I call that function from my class file in the regular php page?

Any thoughts would be appreciated. Thanks in advance.

解决方案

You need to make an instance of the class before calling the functions as they're not static.

require_once('class.php');
$myClass = new myClass();
$myClass-> getAttached();

or, like I said above you could make the function static.

public static function myFunction() {
    //etc...
}

Then to call it you would use:

require_once('class.php');
myClass::getAttached();

这篇关于在我的页面调用PHP类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆