1

The following code is showing the following error:

Warning: require_once(/home/..../public_html/edu) [function.require-once]: failed to open stream: Success in /home/..../public_html/edu/index.php on line 25

Fatal error: require_once() [function.require]: Failed opening required '' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/..../public_html/edu/index.php on line 25

How can I solve this problem?

<?php
    class Model
    {
        public $tstring;
    public function __construct(){
        $this->tstring = "The string has been loaded through the template.";
        $this->template = "tpl/template.php";
    }
}

class View
{
    private $model;

    public function __construct($model) {
        $this->controller = $controller;
        $this->model = $model;
    }

    public function output(){
        $data = "<p>" . $this->model->tstring ."</p>";
        require_once($this->model->template);   //line 25 Attention!!!!!!!!
    }
}


class Controller
{
    private $model;

    public function __construct($model){
        $this->model = $model;
    }

    public function clicked() {
        $this->model->string = "Updated Data, thanks to MVC and PHP!";
    }
}



$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);


echo $view->output();
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Which is line 25 - why are you apparently trying to `require_once` a file named "Success" (These questions are rhetorical). Some basic debugging should resolve your own question – AD7six May 13 '14 at 14:28
  • line 25: require_once($this->model->template); Hi, AD7six! I think the problem is not about the 'success' file. – user3111893 May 13 '14 at 14:32
  • `var_dump($this->model->template)` right before the `require_once`. What do you get? – deceze May 13 '14 at 14:37
  • In this particular code, you certainly have a confusion over your variables in `View::__construct`. Debug! – deceze May 13 '14 at 14:40
  • I changed the line "$this->template = "tpl/template.php";" to $this->template = "/var/www/html/edu/tpl/template.php"; Still the problem exist. Please help. – user3111893 May 13 '14 at 17:25

1 Answers1

0

I find things are greatly simplified when you use relative or absolute/relative paths - so you get to the file you're requiring via the file that requires it, instead of the root directory.

For instance, let's say you have a setup like this:

--/ | | --var | |--www | |--html | |--index.php | |--includes | |--util.php

A client runs the index.php file. Let's say that index.php needs to include util.php are three ways to do this (the following lines would exist in index.php)

Absolute:

require_once('/var/www/html/includes/util.php');

Relative:

require_once('./includes/util.php');

Absolute/Relative:

require_once(dirname(__FILE__).'/includes/util.php');

As you can see, the absolute method addresses the util.php file starting from the / directory. The relative method addresses util.php by starting from index.php, and supplying the path that gets from index.php to util.php. The absolute/relative method actually RESOLVES to the absolute method because the string dirname(__FILE__).'/includes/util.php' will actually resolve to "/var/www/html/includes/util.php", but the functionality still appears as relative because you only need to think about the path that gets from index.php to util.php.

I tend to prefer the absolute/relative approach. Instead of $this->template = "tpl/template.php" you may want to try

$this->template = dirname(__FILE__).'/tpl/template.php';

Even if you still have a problem at this point, you can echo your $this->template variable in order to see exactly what file you are addressing.

Confused about the dirname(__FILE__) part? Here are some links.

dirname

http://ca1.php.net/dirname

__FILE__

http://us2.php.net/manual/en/language.constants.predefined.php

Hope this helps and good luck!

Gershom Maes
  • 7,358
  • 2
  • 35
  • 55