【发布时间】: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 应用程序初始化时自动加载您的类。这可以使用例如 PSR-4 类加载器约定来实现,您可以使用 Symfony2 自动加载器symfony.com/doc/current/components/class_loader/…
标签: php oop namespaces