문제

I can't quite figure out the meaning of this statement:

set_include_path('.'
. PATH_SEPARATOR . '../library/'
. PATH_SEPARATOR . '../application'
. PATH_SEPARATOR . get_include_path());

A quick breakdown would be appreciated.

도움이 되었습니까?

해결책

It adds the two paths to the include_path so that if you include a file "../library/filename.php". you can do it by

include('filename.php');

instead of

include('../library/filename.php');

I suppose this is a part of some framework

It basically adds the folder to the php include path

다른 팁

The first thing to note here is that the constant PATH_SEPARATOR is a predefined constant which allows for a cross-platform path separator (it resolves to ':' on unix-like systems and ';' on windows).

The following code would also achieve the same result but is a bit easier to read:

<?php
$paths = array('.', '../library/', '../application', get_include_path());
set_include_path(join(PATH_SEPARATOR, $paths));

or a bit more verbose, but easy to add to:

<?php

$paths[] = '.';
$paths[] = '../library/';
$paths[] = '../application';
$paths[] = get_include_path();

set_include_path(join(PATH_SEPARATOR, $paths));

What does php's set_include_path function do?

It sets a possible location for the php engine to look for files.

For example:

I put this in a php file called cmp.php under /home1/machines/public_html

<?php
  print "1<br>";
  require("hello.php");
  print "<br>2<br>";

  set_include_path("/home1/machines/public_html/php");

  print "<br>3<br>";
  require("hello.php");
  print "<br>4<br>";
?>

Make a new file hello.php under /home1/machines/public_html, put this in there:

<?php
print "hello from public_html";
?>

Make a second new file called hello.php under /home1/machines/public_html/php, put this in there:

<?php
print "hello from public_html/php";
?>

Run cmp.php, and you should get this:

enter image description here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top