我使用此代码时出错
致命错误:第8行/home/u448966927/public_html/Myclass.php中只能通过引用传递变量
Myclass.php
<?php
class yt
{
public $channels = array
(
"music" => array( "id" => 74,"img" => "<img src=../icon/music.png height=18 width=18 />", "name" => "Music", "slug" => "music") ,
"sports" => array( "id" => 11,"img" => "<img src=../icon/sport.png height=18 width=18 />", "name" => "Sports", "sug" => "sport"),
"movies" => array( "id" => 45,"img" => "<img src=../icon/movies.png height=18 width=18 />", "name" => "Movies", "sug" => "movie"),
"gaming" => array( "id" => 85,"img" => "<img src=../icon/game.png height=18 width=18 />", "name" => "Gaming", "sug" => "game"),
"people" => array( "id" => 56,"img" => "<img src=../icon/people.png height=18 width=18 />", "name" => "People & Blog", "sug" => "people"),
"comedy" => array( "id" => 72,"img" => "<img src=../icon/comedy.png height=18 width=18 />", "name" => "Comedy", "sug" => "comedy"),
"news" => array( "id" => 84,"img" => "<img src=../icon/news.png height=18 width=18 />", "name" => "News & Politics", "sug" => "news"),
"animation" => array( "id" => 29,"img" => "<img src=../icon/film.png height=18 width=18 />", "name" => "Film & Animation", "sug" => "animation"),
"auto-vehicles" => array( "id" => 59,"img" => "<img src=../icon/auto.png height=18 width=18 />", "name" => "Autos & Vehicles", "sug" => "auto-vehicles"),
"howto" => array( "id" => 68,"img" => "<img src=../icon/howto.png height=18 width=18 />", "name" => "Howto & Style", "sug" => "howto"),
"science" => array( "id" => 98,"img" => "<img src=../icon/science.png height=18 width=18 />", "name" => "Science & Technology", "sug" => "science")
);
public function getChannels(){
$html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
ksort(channels);
foreach (channels as $channel) {
$html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
}
return $html;
}
};
?>]
的index.php
<?php include_once('Myclass.php'); echo getChannels(); ?>
答案 0 :(得分:1)
从
更改此段代码<?php include_once('ytcx.php'); echo getChannels(); ?>
要
<?php
include_once('ytcx.php');
$obj = new yt();
echo $obj->getChannels();
?>
我认为PHP认为'
是传递的参数,&
是PHP中的引用字符,它只是让人感到困惑。
您还需要更改
public function getChannels(){
$html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
ksort($this -> channels);
foreach ($this -> channels as $channel) {
$html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
}
return $html;
}
当您从类方法中引用类属性时,您需要$this->
。
答案 1 :(得分:1)
channels
在您的ksort
和foreach
(第22行)中不存在且无效
更改
ksort(channels);
foreach (channels as $channel) {
要
ksort($this->channels);
foreach ($this->channels as $channel) {
由于函数getChannels
在一个类中,您需要instantiate the class。在index.php
中添加以下内容;
$objYT = new yt;
echo $objYT->getChannels();
答案 2 :(得分:0)
试试这个..
class yt
{
public $channels = array
(
"music" => array( "id" => 74,"img" => "<img src=../icon/music.png height=18 width=18 />", "name" => "Music", "slug" => "music") ,
"sports" => array( "id" => 11,"img" => "<img src=../icon/sport.png height=18 width=18 />", "name" => "Sports", "slug" => "sport"),
"movies" => array( "id" => 45,"img" => "<img src=../icon/movies.png height=18 width=18 />", "name" => "Movies", "slug" => "movie"),
"gaming" => array( "id" => 85,"img" => "<img src=../icon/game.png height=18 width=18 />", "name" => "Gaming", "slug" => "game"),
"people" => array( "id" => 56,"img" => "<img src=../icon/people.png height=18 width=18 />", "name" => "People & Blog", "slug" => "people"),
"comedy" => array( "id" => 72,"img" => "<img src=../icon/comedy.png height=18 width=18 />", "name" => "Comedy", "slug" => "comedy"),
"news" => array( "id" => 84,"img" => "<img src=../icon/news.png height=18 width=18 />", "name" => "News & Politics", "slug" => "news"),
"animation" => array( "id" => 29,"img" => "<img src=../icon/film.png height=18 width=18 />", "name" => "Film & Animation", "slug" => "animation"),
"auto-vehicles" => array( "id" => 59,"img" => "<img src=../icon/auto.png height=18 width=18 />", "name" => "Autos & Vehicles", "slug" => "auto-vehicles"),
"howto" => array( "id" => 68,"img" => "<img src=../icon/howto.png height=18 width=18 />", "name" => "Howto & Style", "slug" => "howto"),
"science" => array( "id" => 98,"img" => "<img src=../icon/science.png height=18 width=18 />", "name" => "Science & Technology", "slug" => "science")
);
public function getChannels(){
$html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
ksort($this->channels);
foreach ($this->channels as $channel) {
$html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
}
return $html;
}
}
-----关于index.php ---
include_once('Myclass.php');
$yt = new yt;
echo $yt->getChannels();
答案 3 :(得分:0)
您发布的代码中存在多个问题。
ksort(channels);
foreach (channels as $channel) {
PHP中的变量名称以$
开头。当PHP达到channels
时,它认为它是使用define()
创建的常量。但是函数`ksort()需要通过引用传递的类型数组的参数。这是错误的来源。
如果将其更改为ksort($channels)
,则仍然是错误,因为当前范围中没有定义变量$channels
。你可能需要的是使用当前对象的$channels
属性,并且总是使用$this
(当前对象),然后是->
(成员访问运算符)和您要访问的媒体资源的名称,不包含$
(channels
):
ksort($this->channels);
下一行也一样:
foreach ($this->channels as $channel);
index.php
:
<?php include_once('Myclass.php'); echo getChannels();
对getChannels()
的调用尝试执行全局函数getChannels()
但它失败,因为不存在这样的函数。您要完成的是调用类getChannels()
的对象的方法yt
。为此,您必须首先创建这样的对象:
<?php
include_once 'Myclass.php';
$obj = new yt();
echo $obj->getChannels();
// That's all
文件Myclass.php
应如下所示:
<?php
class yt
{
// ...
public function getChannels(){
$html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
ksort($this->channels);
foreach ($this->channels as $channel) {
$html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
}
return $html;
}
}