Variable as static function - php

I have a class Sections:
class Sections {
public static function get($name) {
//
}
}
And I would like to call the static function get() from it with a variable (http://php.net/manual/en/functions.variable-functions.php):
$section = 'Sections::get';
$section('body');
But it gives me a Fatal error: Call to undefined function Sections::get()
Is it possible to call a static function in this way?
Thanks!

You need to store the class separately from the method:
$class = 'Sections';
$method = 'get';
Now you can call it like this:
$class::$method('body');

Try using call_user_func to do this:
$section = array('Sections', 'get');
call_user_func($section, 'body');
Or:
$section = 'Sections::get';
call_user_func($section, 'body');

I've solved it with the help of a function outside of the class:
class Sections {
public static function get($name) {
//
}
}
function section($name) {
Sections::get($name);
}
Now I can do:
$section = 'section';
$section('body');

Related

Call a static method of class from another class width '$this'

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().

Using global variable in my class causing unexpected syntax

I have a class that requires a variable that is defined out of the scope of it. So i tried using global but, this causes this error:
syntax error, unexpected 'global' (T_GLOBAL), expecting function (T_FUNCTION)
I am unsure if I have put it in the wrong place or using the global keyword incorrectly.
My code looks like this:
$data = new testClass();
class System
{
private $values;
global $data;
public function __construct()
{
}
public function test()
{
return $data->get();
}
}
$system = new System();
echo $system->test();
So i was wondering how do I get the $data variable to be defined in my class? My use of global seems to be incorrect, I also put the global declaration in the __contrust() function but that didn't work either.
Define the global variable within the function instead of the class:
public function test()
{
global $data;
return $data->get();
}
EDIT: Alternate idea:
class System
{
private $values;
private $thedata;
public function __construct($data)
{
$this->thedata = $data;
}
public function test()
{
return $this->thedata->get();
}
}
$data = new testClass();
$system = new System($data);
echo $system->test();
So i was wondering how do I get the $data variable to be defined in my class? My use of global seems to be incorrect, I also put the global declaration in the __contrust() function but that didn't work either.
If you really want to use bad global construction, you should do like this:
class System
{
private $values;
// removed global from here
public function __construct()
{
}
public function test()
{
// added global here
global $data;
return $data->get();
}
}
But OOP principles recommend us to use composition, not global variables. So you can pass the $data into your another class via constructor or via setter. Here's some code implementing both approaches:
class testClass {
public function get()
{
echo __CLASS__.'::'.__FUNCTION__;
}
}
class System
{
private $values;
private $data;
public function __construct(testClass $data = null)
{
if ($data) {
$this->data = $data;
}
}
public function setData(testClass $data)
{
$this->data = $data;
}
public function test()
{
return $this->data->get();
}
}
$data = new testClass();
// via constructor
$system = new System($data);
// or via setter
$system = new System;
$system->setData($data);
echo $system->test();
You could pass $data when you instantiate the class and then assign it in the constructor, which will make it available to all the methods of the class.
class System {
public $data;
public function __construct($data) {
$this->data = $data;
}
public function index() {
echo $this->data;
}
}
$data = 'foo';
$system = new System($data);
echo $system->index();
outputs 'foo';
First things first... This could just be a simple "bad PHP syntax" issue. Look for forgotten ; or in my case... Forgetting that functions actually need the word function : )

Cannot make non static method - FATAL ERROR

I was working on a PHP web application. I used a new datagrid from gurrido.net and it worked well on the local but when I upload it to the server, I get the following error:
Fatal error: Cannot make non static method Base::getClassName() static
in class Singletons in /var/www/reskb/phpinc/Singletons.class.php on
line 84
In my old version where I didn't use the grid, I got it working. Here is my code of singletons.class.php file:
<?
class Singletons extends Base {
var $objects = array();
function getClassName() {
return 'Singletons';
}
function _instance() {
static $_instance = NULL;
if ($_instance == NULL) {
$className = Singletons::getClassName();
$_instance = new $className();
}
return $_instance;
}
function put($object) {
$self = Singletons::_instance();
$className = $object->getClassName();
$self->objects[$className] = $object;
}
function get($className) {
$self = Singletons::_instance();
if(!empty($self->objects[$className]))
return $self->objects[$className];
else return '';
}
}
Singletons::_instance();
?>
You should call function getClassName using object or define getClassName as static. –
<?php
class Singletons extends Base {
var $objects = array();
static function getClassName() {
return 'Singletons';
}
static function _instance() {
static $_instance = NULL;
if ($_instance == NULL) {
$className = Singletons::getClassName();
$_instance = new $className();
}
return $_instance;
}
function put($object) {
$self = Singletons::_instance();
$className = $object->getClassName();
$self->objects[$className] = $object;
}
function get($className) {
$self = Singletons::_instance();
if(!empty($self->objects[$className]))
return $self->objects[$className];
else return '';
}
}
Singletons::_instance();
?>
You're getting the error because you're trying to call your function statically when it isn't a static method (function).
You need to specify the function as static:
static function getClassName() {
return 'Singletons';
}
That goes for every method you'd like to call statically.
If you declare a function as abstract in an abstract super class, then attempt to define it as static in a child class, you will get a fatal error. You may want to think about the way your class hierarchy is structured, as your only option will be to remove the abstract function declaration from the super class.

Undefined Function Error PHP

I'm writing a class and I can't figure out why I am getting this error:
PHP Fatal error: Call to undefined method Directory::BuildDirectoryListing()
in C:\www\directory.php on line 25
It doesn't make any sense. By the error it looks like it is trying to look for a static function. Here is the code I am using:
$odata = new Directory($listing['id']);
$adata = $odata->BuildDirectoryListing();
<?php
include_once("database.php");
class Directory {
public $listing = array();
public $aacategories = array();
function __construct($_listing) {
$this->listing = $_listing;
}
public function BuildDirectoryListing() {
/* function code here */
}
}
?>
Directory is a PHP built-in class.
You need to namespace your code or change your class name:
Class:
<?php
namespace MyApp;
class Directory {
public $listing = array();
public $aacategories = array();
function __construct($_listing) {
$this->listing = $_listing;
}
public function BuildDirectoryListing() {
/* function code here */
}
}
?>
Creating the class:
<?php
$odata = new \MyApp\Directory($listing['id']);
$adata = $odata->BuildDirectoryListing();
?>
You are calling a static function in Directory::BuildDirectoryListing(), change this line
public function BuildDirectoryListing() {
for
public static function BuildDirectoryListing() {

How to pass php variable into a class function?

I want to pass php variable $aa into a class function. I have read some articles
in php.net, but I still don't understand well. Can anyone help me put the variable into this class? thanks.
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct() {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
Simply put the variable names in the constructor.
Take a look at the snippet below:
public function __construct( $aa )
{
// some content here
}
I'm not sure what you mean... do you mean you want to access $aa in a function? If so:
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct() {
global $aa;
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
Or, on a per instance basis, you can do things like:
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct($aa) {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
new Action($aa);
$aa='some word';
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct($aa) {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
And use it like this:
$instance = new Action('something');
I don't know php, but my logic and google say this:
class Action {
private $_objXML;
private $_arrMessages = array();
public function __construct($aa) {
$this->_objXML = simplexml_load_file($aa.'.xml');
}
}
$object = new Action('some word');
This is simply called pass a variable as parameter of a function, in this case the function is the constructor of Action

Categories