using a static class with properties in php - php

I'm trying to get the selected language to appear in a link with the function buildMenu().
I would like to use it as a static function so I can call it in my header template. If I call the function in the init() function it all works fine, however, when I try to use it as a static function, nothing works anymore. I've tried everything I know, so it seems my knowledge of php ends there :)
Any of you got any tips? Thanks in advance!
class bootstrap {
static public $lang;
static public $class;
static public $method;
public function init(){
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
//Set values on startup
if($url[0] == NULL) {$url[0] = 'nl';}
if($url[1] == NULL) {$url[1] = 'index';}
if(isset($url[0])) {$this->lang = $url[0];}
if(isset($url[1])) {$this->class = $url[1];}
if(isset($url[2])) {$this->method = $url[2];}
$this->loadClass();
}
public function loadClass(){
$filename = 'libs/' . $this->class . '.php';
if(file_exists($filename)){
$newController = new $this->class($this->lang, $this->class, $this->method);
$newView = new view($this->lang, $this->class, $this->method);
} else {
$newclass = new error($this->lang, $this->class, $this->method);
}
}
public function buildMenu(){
echo '<li>Foto</li>';
}
/*
* Set paths
*/
public static function root(){
echo "http://localhost/testing/";
}
}

You are using the object operator (->) instead of the scope resolution operator (::) that is used to reference class constants and static properties or methods.
See here for an explanation of the static keyword and working with static properties.
Update your code to this:
class bootstrap{
static public $lang;
static public $class;
static public $method;
public function init(){
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
//Set values on startup
if($url[0] == NULL) {$url[0] = 'nl';}
if($url[1] == NULL) {$url[1] = 'index';}
if(isset($url[0])) {self::$lang = $url[0];}
if(isset($url[1])) {self::$class = $url[1];}
if(isset($url[2])) {self::$method = $url[2];}
$this->loadClass();
}
public function loadClass(){
$filename = 'libs/' . self::$class . '.php';
if(file_exists($filename)){
$newController = new self::$class(self::$lang, self::$class, self::$method);
$newView = new view(self::$lang, self::$class, self::$method);
} else {
$newclass = new error(self::$lang, self::$class, self::$method);
}
}
public static function buildMenu(){
echo '<li>Foto</li>';
}
public static function root(){
echo "http://localhost/testing/";
}
}

As #elclanrs has already mentioned, how about changing the buildMenu() method to
public static function buildMenu(){
echo '<li>Foto</li>';
}
You can then call it using bootstrap::buildMenu().

Related

How i can create child view for parent view in PHP MVC?

When i type this address:
http://localhost/gamelvl/world-of-tanks/tankguide/
I can enter the tankguide view. Now inside this folder is another folder called ussr but when I create a controller and model for ussr I can not enter it with this address:
http://localhost/gamelvl/world-of-tanks/tankguide/ussr/
Now, any one can give me instructions on whether this is the right thing or other solutions for this?
routing app
<?php
class App
{
public $controller = 'index';
public $method = 'index';
public $params = [];
function __construct()
{
if (isset($_GET['url'])) {
$url = $_GET['url'];
$url = $this->parseUrl($url);
$this->controller = $url[0];
unset($url[0]);
if (isset($url[1])) {
$this->method = $url[1];
unset($url[1]);
}
$this->params = array_values($url);
}
$controllerurl = 'controllers/' . $this->controller . '.php';
if (file_exists($controllerurl)) {
require($controllerurl);
$object = new $this->controller;
$object->model($this->controller);
if (method_exists($object, $this->method)) {
call_user_func_array([$object, $this->method], $this->params);
}
}
}
function parseUrl($url)
{
filter_var($url, FILTER_SANITIZE_URL);
$url = rtrim($url, '/');
$url = explode('/', $url);
return $url;
}
}
?>
tankguide controller
<?php
class Tankguide extends Controller
{
function index()
{
$this->view('tankguide/index');
}
}
?>
core controller
<?php
class Controller
{
function __construct()
{
}
function view($viewUrl,$data=[])
{
require('header.php');
require('views/' . $viewUrl . '.php');
require('footer.php');
}
function model($modelUrl)
{
require('models/model_' . $modelUrl . '.php');
$classname = 'model_' . $modelUrl;
$this->model = new $classname;
}
}
?>
project structure
if (method_exists($object, $this->method)) will return false for the given url, because it is looking for a method called ussr on the Tankguide class. Either create a ussr method on that class

PHP MVC - return call_user_func_array() in a __construct

I am working on a MVC site, but I have a problem. I can not return anything from my controllers. I can do things like $this->someMethod() but not return "hello" I think it has to do with call_user_func_array() So how do I fix this?
My code:
Router:
Class App
{
protected $controller = 'home';
protected $method = 'index';
protected $parameters = [];
public function __construct()
{
$url = $this->Url();
if(file_exists('../app/controllers/'. $url[0] .'.php')){
$this->controller = $url[0];
unset($url[0]);
}
require_once '../app/controllers/'. $this->controller .'.php';
$this->controller = new $this->controller;
if (isset($url[1])){
if (method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
}
$this->parameters = $url ? array_values($url) : [''];
call_user_func_array([$this->controller, $this->method], $this->parameters);
}
public function Url()
{
if(isset($_GET['url'])){
$url = filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL);
$exploded_url = explode('/', $url);
return $exploded_url;
}
}
}
Public Index.php:
<?php
require_once '../app/core/init.php';
$app = new App;
A controller:
class Home extends Controller
{
public function index($name)
{
//this works.
$this->view('index');
//this does not work.
return "hello";
}
}
So how do I solve it, so the return works too?

PHP: calling require_once in constructor function

require_once in constructor function, is not working I also tried include and require. file_exists function is working fine.
Here is my code:
<?php
class App {
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
public function __construct() {
$url = $this->parseUrl();
//print_r($url);
if(file_exists(__DIR__.'/../controllers/'. $url[0] . '.php')) {
$this->controller = $url[0];
unset($url[0]);
}
require_once(__DIR__.'/../controllers/Home.php');
echo $this->controller;
}
}

Dynamic instantation of namespaced methods in php

There are few routers out there but I decided to create a very simple route for a very light site.
Here is my index.php
$route = new Route();
$route->add('/', 'Home');
$route->add('/about', 'About');
$route->add('/contact', 'Contact');
Here is my router:
<?php namespace Laws\Route;
use Laws\Controller\Home;
class Route
{
private $_uri = array();
private $_method = array();
private $_route;
public function __construct()
{
}
public function add($uri, $method = null)
{
$this->_uri[] = '/' . trim($uri, '/');
if ($method != null) {
$this->_method[] = $method;
}
}
public function submit()
{
$uriGetParam = isset($_GET['uri']) ? '/' . $_GET['uri'] : '/';
foreach ($this->_uri as $key => $value) {
if (preg_match("#^$value$#", $uriGetParam)) {
$useMethod = $this->_method[$key];
new $useMethod(); // this returns an error (cannot find Home'
new Home(); // this actually works.
}
}
}
}
new $useMethod(); does not work. returns error 'cannot find Home'
new Home(); actually works.
What am I missing here?
You can use your concurrent way for calling a class or you can use this:
call_user_func(array($classname,$methodname))

Can't access property from outside function PHP?

I am trying to access a property outside of a function in PHP, but it is not working?
<?php
class Bootstrap {
private $_url = null;
public function init() {
$this->_loadController();
}
public function getUrl() {
$url = (isset($_GET['url'])) ? $_GET['url'] : null;
$this->_url = explode('/', $_GET['url']);
}
public function _loadController() {
echo $this->_url[0];
}
}
If I echo $this->_url[0] in the method getUrl and then call it from my index page, I get the URL. While if I call it from the _loadController method, I just get a blank page. I did var_dump($this->_url) and got NULL.
My code in my index page is follows
<?php
include 'bootstrap.php';
$bootstrap = new Bootstrap();
$app = $bootstrap->init();
?>
Could someone help me out? Thanks!
You have the problem because, you did not call getUrl(), but in this method you set valueinto $_url. Change you code to this:
class Bootstrap {
private $_url = null;
public function init() {
$this->_loadController();
}
public function getUrl() {
$url = (isset($_GET['url'])) ? $_GET['url'] : null;
$this->_url = explode('/', $_GET['url']);
}
public function _loadController() {
$this-> getUrl(); // here you call getUrl()
echo $this->_url[0];
}
}
Or, you can use magic mathod to set the url into, here:
class Bootstrap {
private $_url = null;
public function init() {
$this->_loadController();
}
public function __construct() {
$url = (isset($_GET['url'])) ? $_GET['url'] : null;
$this->_url = explode('/', $_GET['url']);
}
public function _loadController() {
echo $this->_url[0];
}
}

Categories