duanpacan9388 2014-05-19 10:41
浏览 36
已采纳

调用未定义的函数[重复]

This question already has an answer here:

I'm currently working on a application which pulls JSON from a database and then adds a new user to the array and returns it as JSON. When I tried running my addFriend() function I got the "Call to undefined function userExists()" error. I can't seem to figure out why.

public function userExists($user = null){
        if(is_numeric($user)){
            $test = $this->_db->get('members', array('ID', '=', $user));
            if($test->count()) {
                return true;
            }
        }
        return false;
}

public function addFriend($friend){
        if(is_numeric($friend)){
            if( userExists($friend) ){
                if(hasFriends()){
                    $friendsList = getFriends();
                    $friendsList[] = $friend;
                }else{
                    $friendsList = array($friend);
                }

            }
        }
        return false;
    }

I am using a PDO class I made to call the queries and make the connection, which seems to be working fine. count() just returns the rowCount().

I know it's going to be something small I missed but the help will be appreciated!

</div>
  • 写回答

1条回答 默认 最新

  • doufuhuang6981 2014-05-19 10:43
    关注

    If you are inside the class, replace

    if( userExists($friend) ){
    

    with

    if( $this->userExists($friend) ){
    

    Basically to refer to pretty much anything inside the class, append $this-> to whatever you are calling, a property, a function etc.

    It tells the class what to actually use, ie its own function or its own properties.

    On that note, looking at the rest of your code, rather than using variables, I would suggest creating and using properties of the class.

    For example, you have a variable called $friendsList in your addFriend function. It might be better to declare the class with a property called $friendList like this:

    class someThing{
    
    public $friendlist;
    
    // etc
    }
    

    and then in your functions, append or delete data from that property like this:

            if( userExists($friend) ){
                    $this->friendsList = getFriends();
                    $this->friendsList[] = $friend;
                }else{
                    $this->friendsList = array($friend);
                }
    

    In this way, once you assign the values, they are there and available for any other call or function to use as needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法