English 中文(简体)
• 如何将一个班级列入PHP[封闭式]
原标题:How to include a class in PHP [closed]

要求制定法典的问题必须 尽可能少地理解正在解决的问题。 包括尝试性解决办法,为什么他们做t工作,以及expected 结果。 另见:。 排出问题清单

Closed 9 years ago.

我有文件index.php,我希望在文件中列入文件class.twitter.php。 我如何能够这样做?

我希望,在我把以下法典列入指数时。 该中心将开展工作。

$t = new twitter();
$t->username =  user ;
$t->password =  password ;

$data = $t->publicTimeline();
最佳回答

你的法典应该像现在这样。

require_once( class.twitter.php );

$t = new twitter;
$t->username =  user ;
$t->password =  password ;

$data = $t->publicTimeline();
问题回答

Include a class example with the use keyword from Command Line Interface:

PHP 姓名空间在指挥线上不工作,除非你也包含或要求存放地点档案。 当网址上出现由Sphp daemon解释的网址时,你不需要所要求的线。 你们都需要的是使用线。

  1. 创建新的名录<代码>/home/el/bin。

  2. 制作一个新文件,称为 姓名space_example.php。 并将该守则列入其中:

    <?php
        require  /home/el/bin/mylib.php ;
        use foobarwhateverdingdongpenguinclass;
    
        $mypenguin = new penguinclass();
        echo $mypenguin->msg();
    ?>
    
  3. 另有一份名为<条形码>。

    <?php
    namespace foobarwhateverdingdong;
    class penguinclass 
    {
        public function msg() {
            return "It s a beautiful day chris, come out and play! " . 
                   "NO!  *SLAM!*  taka taka taka taka."; 
        }   
    }
    ?>   
    
  4. 从指挥系统来看:

    el@apollo:~/bin$ php namespace_example.php 
    
  5. 印刷:

    It s a beautiful day chris, come out and play!
    NO!  *SLAM!*  taka taka taka taka
    

见以下评论中关于这一问题的说明:http://php.net/manual/en/English.namespaces.importing.php

您可以使用以下两种方法:

include "class.twitter.php";

require "class.twitter.php";

Using require (或 require_once if you want to ensure the class is only loaded once during execution) will cause a fatal err或 to be raised if the file doesn t exist, whereas include will only raise a warning. See http://php.net/require and http://php.net/include f或 m或e details

I suggest you also take a look at __autoload.
This will clean up the code of requires and includes.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...