I'm new to PHP, I'm trying to require UserController.php from Controller.php but all I get is "HTTP ERROR 500" in browser. What's going on here?
Controller.php
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
require_once "../Controllers/UserController.php";
}
}
UserController.php
class UserController
{
public function __construct()
{
echo '111111111';
}
public function hi(){
echo '1';
}
}
$a = new UserController();
$a->hi();
Class definitions can't be nested inside functions or other classes. So you shouldn't have that require_once line inside a function definition. Move it outside the class.
require_once "../Controllers/UserController.php";
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
}
}
<?php
require_once "../Controllers/UserController.php";
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
$a = new UserController();
$a->hi();
}
}
Related
I am getting this error
"Fatal error: Uncaught Error: Call to a member function generate() on null in /var/www/example.loc/app/Controllers/ControllerMain.php on line 17"
when calling the action_index function;
This is the ControllerMain.php file; general_view.php is my layout;
<?php
namespace App\Controllers;
use App\Models\Users;
use Core\Controller;
class ControllerMain extends Controller
{
public function __construct()
{
$this->model = new Users();
}
public function action_index()
{
$this->view->generate('general_view.php');
}
File Core\Controller
<?php
namespace Core;
class Controller {
public $model;
public $view;
public function __construct()
{
$this->view = new View();
}
public function action_index()
{
}
}
File Core/view.php
<?php
namespace Core;
class View
{
//public $template_view; // здесь можно указать общий вид по умолчанию.
function generate($general_view, $data = null)
{
if(is_array($data)) {
// преобразуем элементы массива в переменные
extract($data);
}
include 'app/views/'.$general_view;
}
}
I could not find the right solutions for myself, so I decided to write here, so I hope for your help
I ask you to help solve the problem, because I am new to php, and sorry for my bad english :)
Perhaps I did not give all the information to understand what was going on, so if you need something, write to me
Please correct me if I'm wrong, I guess you need parent::__construct(); in ControllerMain.
Example:
class ControllerMain extends Controller
{
public function __construct()
{
parent::__construct();
$this->model = new Users();
}
public function action_index()
{
$this->view->generate('general_view.php');
}
I have two php classes in two separate files:
File Name : ld.php
<?php
class LandDetail_Model{
public function __construct() {}
private $id;
public $pId;
private $bigha;
private $katha;
function setId($id) { $this->id = $id; }
function getId() { return $this->id; }
function setPId($pId) { $this->pId = $pId; }
function getPId() { return $this->pId; }
function setBigha($bigha) { $this->bigha = $bigha; }
function getBigha() { return $this->bigha; }
function setKatha($katha) { $this->katha = $katha; }
?>
File Name :Land_Detail.php
<?php
require_once '../models/ld.php';
class LandDetail extends LandDetail_Model{
public function __construct() {
parent::__construct();
}
public function DoSomething(){
echo "This is a value from Parent".$this->getPId();
}
}
?>
Now in SomeFile.php I am doing something like this.
<?php
include 'Land_Detail.php';
$ld = new LandDetail();
$ld->setPId(10001);
$ld->DoSomething();
?>
Why $this->getPId() is always returning null value? What is wrong with my code here? What is the correct way to extend a class in php from different file?
Why you write:
$ld->setPId = 10001;
Instead of
$ld->setPId(10001);
I have this code and i´m trying to use a object
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
?>
i would like to do this:
class Index extends Controller {
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
i would like to use the set_parms function.
but i can't see the View Function, then i can not use.
Can someone explain and advise me a good and safe way?
Correction from Phil: If a __construct() method isn't found, PHP will revert to legacy constructor syntax and check for a method with the same name as the object. In your case the method index() is being treated as the constructor, and is preventing the parent's constructor from loading the view object into the $_view property.
You can force a class to inherit a parent's constructor by defining __construct() in the child and calling the parent's constructor:
public function __construct() {
parent::_construct();
}
Here is the fixed code:
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
.
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
.
class Index extends Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
I'm trying to find a solution of my problem, but unsuccessfully. For example:
I have one main class:
class System {
public function loadClass($instance, $name)
{
$this->$instance = new $name;
}
}
And I want, that I simply can load another class into main class called System.
<?php
$system = new System;
$system->loadClass('db', 'database');
?>
It's working.. But I need to access all methods (and instances) of loaded class from any loaded class like this:
<?php
class System {
public function loadClass($instance, $name)
{
$this->$instance = new $name;
}
public function run()
{
echo $this->subA->methodA();
echo $this->subB->methodB();
}
}
class subClassA extends System {
function __construct()
{
echo $this->subB->methodB();
}
public function methodA()
{
return 'okA';
}
}
class subClassB extends System {
function __construct()
{
echo $this->subA->methodA();
}
public function methodB()
{
return 'okB';
}
}
$system = new System;
$system->loadClass('subA', 'subClassA');
$system->loadClass('subB', 'subClassB');
$system->run();
?>
How it is possible without using static methods and instances?
In the index file i have _autoload and load the libs and then i explode the url to get the wanted contoller and the model if exists. In the view i can see the model __construct() so the model is loaded but if i try to use $this->model->test(); i get
Call to a member function test() on a non-object
http://site.com/about
$this->request = about;
$controller = new $this->request;
$controller->loadModel($this->request);
Everething work ok
*Here is the Main controller *
class Conroller {
function __construct() {
// echo 'Main controller<br />';
$this->view = new View();
}
public function loadModel($name) {
$path = 'models/'.$name.'_model.php';
if (file_exists($path)) {
require 'models/'.$name.'_model.php';
$modelName = $name . '_model';
// **here i make the object**
$this->model = new $modelName();
}
}
}
Here is the About model
class about_model{
function __construct() {
echo 'test';
}
public function test() {
$test = 'test one';
}
}
Here is the About Conroller
class About extends Conroller {
function __construct(){
parent::__construct();
$this->model->test();
$this->view->render('/about');
}
}
You will need to call loadModel in your About controller before you refer to the model:
class About extends Conroller {
function __construct(){
parent::__construct();
$this->loadModel('about');
$this->about->test();
}
}