【问题标题】:how to use namespace and oop concept in php如何在 php 中使用命名空间和 oop 概念
【发布时间】:2015-05-14 11:42:50
【问题描述】:

我试图访问另一个类中的另一个类方法,例如基于 oops,但我没有成功,我在这里描述了我的问题
我创建一个命名空间 像下面的目录 目录:Tets/test.php 及其代码如下:

<?php
        namespace Test;
        final class TestClient
        {
            public static function GetStoryById($fb_user_id)
            {   
            $all_story=array();
            $query="select * from story where fb_user_id='".$fb_user_id."' order by story_id desc";
            $query_run=  mysql_query($query);
            echo mysql_error();
            $num_rows=  mysql_num_rows($query_run);
            if($num_rows>0)
                {
                $fb_user_id=$data['fb_user_id'];
                while($row=  mysql_fetch_assoc($query_run))
                {
                   $all_story['story_id']=$row['story_id'];
                   $all_story['fb_user_id']=$row['fb_user_id'];
                   $all_story['tagline']=$row['tagline'];
                   $all_story['experience']=$row['experience'];
                   $all_story['category_type']=$row['category_type'];
                   $all_story['lat']=$row['lat'];
                   $all_story['log']=$row['log'];
                   $all_story['datetime']=$row['datetime'];
                   $all_story['last_update_at']       =$row['last_update_at'];
                }
                }
                if(!empty($all_story['story_id']))
                {
                 $all['all_story']=$all_stories;
                 $all['statusmessage']="ok";
                 $all['statuscode']="1";
                }
                else{
                   $all['statusmessage']="there is no story found";   
                  $all['statuscode']="2";
                }
                $response['response']=  array_reverse($all);
                $deatils=json_encode($response);
             return $details;
            }
            }


I need to access its Function GetStoryById
in another php file which is like 
--get.php
and code like


<?php

define('Tets-SDK-DIR', './Tets');
use Test\TestClient;

class facebook_details {

     public function user_detail_with_story($data)
     {
       $query="select * from user where fb_user_id='".$data['fb_user_id']."'";
       $query_run=  mysql_query($query);
       $num_rows=  mysql_num_rows($query_run);
       if($num_rows>0)
       {
           $row=  mysql_fetch_assoc($query_run);
           $status['user_details']=$row;
           $fb_user_id= $data['fb_user_id'];
         $status['all_details']=TestClient::GetStoryById($fb_user_id); 
       }
       echo json_encode($status);

     }  
}

$facebook_details=new facebook_details();

if($_REQUEST['action']=='user_detail_with_story')
{
    $facebook_details->user_detail_with_story($_REQUEST);
}

当我尝试通过提供 action=user_detail_with_story&user_id=10 来通过 url 访问时,例如 我得到了胎儿错误 致命错误:在第 16 行中找不到类“Test\TestClient”

请帮助我如何通过任何方法在另一个类中使用另一个类对象,所以我确实需要一次又一次地编写方法

我会等待回复

谢谢

【问题讨论】:

标签: php oop namespaces


【解决方案1】:

我解决了我的问题,只需在此行代码上方使用此代码include './Test/Test.php'; 或在课前 $status['all_details']=TestClient::GetStoryById($fb_user_id);

【讨论】:

  • 你也可以在php中尝试自动加载
猜你喜欢
  • 2021-08-29
  • 2013-10-17
  • 1970-01-01
  • 2018-03-16
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多