This function is in Application class > mvc > php
private function splitUrl()
{
if (isset($_GET['url'])) {
// split URL
$url = trim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
$this->url_controller = isset($url[0]) ? $url[0] : null;
$this->url_action = isset($url[1]) ? $url[1] : null;
unset($url[0], $url[1]);
$this->url_params = array_values($url);
}
}
New edit
I have this class
class Page extends Controller
{
public function __construct(){
//echo parent::splitUrl()->this->url_action;
echo parent::$this->url_action;
}
public function index()
{
// removed lins
}
}
How to get $this->url_action in extends functions ?
The field url_action should be protected (or public, but that's not recommend). So you can use this in child classes.
See below such as pseudoniem code:
In your parent:
class Foo
{
protected $url_action = 'something'; // or set it via setter
}
The child class:
class FooChild extends Foo // Now, child can access protected (and public) fields of its parent!
{
public function getFromParent()
{
return $this->url_action; // or what you want
}
}
Related
I'am begginer in OOP PHP. I have code like this
class Index
{
public $activepage = true;
public $url;
public $page;
function __construct()
{
if ($this->activepage) {
$this->url = "Yes";
$this->page = "Home";
} else {
$this->url = "No";
$this->page = "Index";
}
}
public function show()
{
return $this->page;
}
public function showTest()
{
return "test";
}
}
class Home extends Index
{
function __construct()
{
echo $this->show();
}
}
$page = new Home;
My questions is :
Why I have blank page when I invoke Home class?
But when I change constructor in Home class like this echo $this->showTest();, it works. and displaying "test" on screen.
and what actually diferrent between my show method and showTest method in Index class?
When you add a __construct() in the Home class it overrides the construct from the parent class Index.
You can invoke the parent construct manually with:
function __construct()
{
parent::__construct();
echo $this->show();
}
You can call Parent class method like;
parent::show()
I've got some problem.
I want to call static method of class from another class.
Class name and method are created dynamically.
It's not really hard to do like:
$class = 'className';
$method = 'method';
$data = $class::$method();
BUT, i want to to do it like this
class abc {
static public function action() {
//some code
}
}
class xyz {
protected $method = 'action';
protected $class = 'abc';
public function test(){
$data = $this->class::$this->method();
}
}
And it doesn't work if i don't assign $this->class to a $class variable, and $this->method to a $method variable.
What's the problem?
In PHP 7.0 you can use the code like this:
<?php
class abc {
static public function action() {
return "Hey";
}
}
class xyz {
protected $method = 'action';
protected $class = 'abc';
public function test(){
$data = $this->class::{$this->method}();
echo $data;
}
}
$xyz = new xyz();
$xyz->test();
For PHP 5.6 and lower you can use the call_user_func function:
<?php
class abc {
static public function action() {
return "Hey";
}
}
class xyz {
protected $method = 'action';
protected $class = 'abc';
public function test(){
$data = call_user_func([
$this->class,
$this->method
]);
echo $data;
}
}
$xyz = new xyz();
$xyz->test();
The object syntax $this->class, $this->method makes it ambiguous to the parser when combined with :: in a static call. I've tried every combination of variable functions/string interpolation such as {$this->class}::{$this->method}(), etc... with no success. So assigning to a local variable is the only way, or call like this:
$data = call_user_func(array($this->class, $this->method));
$data = call_user_func([$this->class, $this->method]);
$data = call_user_func("{$this->class}::{$this->method}");
If you need to pass arguments use call_user_func_array().
I created a parent class (A) and modified some of its public and protected properties.
I created a child class (B) that extends A.
I can see the parent properties at B instance after creating it.
Problem is: The inherited properties of B have the default values of A, from before I modified them.
I want B to hold the modified values of the inherited properties.
How?
class Dashboard {
protected $testBusinessesIds = '';
public function test_bids($a){
$this->testBusinessesIds = $a;
}
}
class DashboardDBHelper extends Dashboard{
protected $withoutTestBids = '';
public function __construct(){
if($this->testBusinessesIds != '')
$this->withoutTestBids = " AND B.`id`";
}
}
$d = new Dashboard();
$d->test_bids(23);
$dh = new DashboardDBHelper();
print_r($dh->withoutTestBids);
I see: '' instead of 'AND B.id'
You may need to put your property as static. Here's an example:
class A {
protected $value = 1;
protected static $staticValue = 1;
public function printStatic() {
self::$staticValue++;
echo self::$staticValue;
}
public function printNonStatic() {
$this->value++;
echo $this->value;
}
}
class B extends A {
public function printStatic() {
echo self::$staticValue;
}
public function printNonStatic() {
echo $this->value;
}
}
$a = new A();
$b = new B();
/* Class A */
$a->printStatic(); // 2
$a->printNonStatic(); // 2
/* Class B */
$b->printStatic(); // 2
$b->printNonStatic(); // 1
Static variables does not share the same class/object so if you modify the value it will be changed everywhere.
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];
}
}
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().