PHP - How can I define "static" variables in a class - php

I'm trying to call some "global/static" variables($agent, $version) inside a class but I don't know how to insert them in the class itself . Basically I declare the class "$myclass = new myClass($agent, $version) but I don't know how to call these variables inside the class (e.g. use them in the getProducts function ). I know the questions sounds stupid but I just don't get it .
index.php :
$myclass = new myClass($agent, $version);
$agent = "firefox";
$version = "234";
$catalog = $myClass->$getProducts("http://amazon.com", "red");
myclass.class.php :
class myClass {
function getXML ($agent, $version) {
//do something
return $
}
function getProducts ($url, $color) {
$product = $this->getXML($agent, $version);
$catalog = str_replace("asus", "", $product);
return $catalog
}
}

It doesn't sound like static variables are what you're looking for and are best avoided unless you know what you're doing (they're not really object oriented and not needed at all in PHP 5.3+). From the code that you've provided it looks like your expecting to pass the arguments into the object instantiation (new), so you should create a constructor for the class that accepts the arguments and assigns them to instance variables to be used in the method.

You can declare them using the static keyword.
From http://php.net/manual/en/language.oop5.static.php :
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
print Foo::$my_static . "\n";

I suppose that you do not mean "static" variables but "properties":
class myClass {
// Declare the properties
private $agent, $version;
public function __construct($agent, $version) {
// Copy the arguments to the class members
$this->agent = $agent;
$this->version = $version;
}
public function getProducts($url, $color) {
$product = $this->getXML();
// ...
}
private function getXML() {
// Use the values stored in $this->agent etc.
$agent = $this->agent;
// ...
}
}
Usage:
$agent = "firefox";
$version = "234";
$instance = new myClass($agent, $version);
$catalog = $instance->getProducts("http://amazon.com", "red");

This is also wrong:
function getProducts ($url, $color){
$product = $this->getXML($agent, $version);
$catalog = str_replace("asus", "", $product);
return $catalog
}
You cant pass $agent and $version variables in the getXML method unless you dont either, construct them first, or pass them in your getProducts method.

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 : )

Trying to figure out static variables in PHP

I have the following class:
class cls_Tip extends cls_getTips {
public static $result = null;
public static $event = null;
public static $market = null;
public static $participant = null;
public function __construct() {
}
public static function grab( $id ) {
global $wpdb;
$query = " SELECT * FROM " . $wpdb->prefix . "asp_tips WHERE id = '" . $id . "';";
$result = $wpdb->get_row( $query );
self::$result = $result;
}
public static function add_element_to_parent_array(){
parent::add_array_ellement( self::$result );
}
public static function return_static_variable( $variable_name ) {
return self::${ $variable_name };
}
public static function get_event() {
new cls_Event;
cls_Event::grab( self::$result->markets_id );
cls_Event::add_static_variable_to_parent();
}
public static function get_market() {
new cls_Market;
cls_Market::grab( self::$result->markets_id );
cls_Market::add_static_variable_to_parent();
}
public static function get_participant() {
new cls_Participant;
cls_Participant::grab( self::$result->participants_id );
cls_Participant::add_static_variable_to_parent();
}
public static function add_static_variable( $variable_name, $class ) {
self::${ $variable_name } = $class;
}
}
When I initiate this class as follows:
new cls_Tip;
cls_Tip::get( $record->id );
cls_Tip::get_participant();
$p = cls_Tip::return_static_variable( 'participant' );
... it works, but $p continues to have the same value, after creating another 'new cls_Tip' as outlined above (in a loop.)
Is this because a static variable can only be set once? And what would you recommend doing in this case?
Thanks very much for your advice.
Best regards,
Giorgio
PS: Here is the cls_Participant class:
class cls_Participant extends cls_Tip {
public static $result = null;
public function __construct() {
}
public static function grab( $id ) {
global $wpdb;
$query = " ... ";
$result = $wpdb->get_row( $query );
self::$result = $result;
}
public static function add_static_variable_to_parent() {
parent::add_static_variable( 'participant', self::$result );
}
}
Well, you see, when you call static properties/methods of a class, unless you explicitly handle the instantiation inside of the function that you call, the "static" instance is a completely separate instance from the instantiated one -
Thus, you should do this:
$class = new cls_Tip;
$class::get( $record->id );
$class::get_participant();
$p = $class::return_static_variable( 'participant' );
And that should give you the behavior you expect...
However, in this case, you don't need to use static methods/properties... you could do:
$class = new cls_Tip;
$class->get( $record->id );
$class->get_participant();
$p = $class->return_static_variable( 'participant' );
Furthermore, this is equivalent, in your last line:
$p = cls_Tip::$participant
You don't need the getter function, since the property is public...
BUT, to further illustrate my answer, do this:
cls_Tip::$result = "Static Result";
$alt = new cls_Tip();
$alt::$result = "Instantiated Property";
echo cls_Tip::$result;
echo $alt::$result;
So, ultimately, the lesson here is that if you are going to have several separate instances of a class, you don't really need to label everything as static -
There seems to be a lot of discourse on this topic - some people say you should almost never use static methods/properties in PHP, albeit they seem to have a proper place in some cases.
However, it is my personal opinion that you would be better off taking all the static stuff out of your class, which is going to have several instances.
Thus, you would end up using the -> operator instead of the static :: operator.
A case where the :: operator would be more appropriate would be if you wanted to have a static class that managed all your instances might look like this...
class clp_Tip{
static private $instances;
//[...]
public static function new_instance($name){
return self::$instance[$name] = new $this;
}
public static function get_instances(){
return self::$instances;
}
//[...]
}
//[...]
// example:
$classOne = cls_Tip::new_instance('name');
$classTwo = cls_Tip::new_instance('two');
echo count(cls_Tip::get_instances()); // 2
$classOne->doSomeFunction();
$classOne->someProperty = "foo";
}
There are plenty of debates over why not use :: - the simplest answer is simply for design purposes, but - it makes good sense to use the operators in the way they were made to be used -
and the static operators were made to be used without having to invoke a new instance -
So, that is good enough reason, in my opinion, to remove the static stuff from your class and use the -> operator.

Getting class of variable

How can I figure out in what class a reference to a variable was initiated (and currently exists)?
Example:
<?php
class MyClass {
public $array = array(
"this",
"is",
"an",
"array"
);
}
$class = new MyClass();
$arrayReference = &$class->array;
GetClassForVariable($arrayReference); //Should return "MyClass"
?>
My best bet is some kind of Reflection, but I haven't found any functions that seem suitable for this.
Edit:
A better suited example for what I want is the following:
<?php
class API_Module {
public $module;
public $name;
private $methods = array();
public function __construct($module, $name) {
$this->module = $module;
$this->name = $name;
$this->methods["login"] = new API_Method($this, "login", "Login");
}
public function GetMethod($method) {
return $this->methods[$method];
}
public function GetURL() {
return $this->module; //Should return "session"
}
}
class API_Method {
public $method;
public $name;
private $parentReference;
private $variables = array();
public function __construct(&$parentReference, $method, $name) {
$this->parentReference = $parentReference;
$this->method = $method;
$this->name = $name;
$this->variables["myvar"] = new API_Variable($this, "myvar");
}
public function GetURL() {
return $this->GetParentURL() . "/" . $this->method; //Should return "session/login"
}
public function GetVariable($variableName) {
return $this->variables[$variableName];
}
private function GetParentURL() {
// Need to reference the class parent here
return $this->parentReference->GetURL();
}
}
class API_Variable {
public $name;
private $parentReference;
public function __construct(&$parentReference, $name) {
$this->parentReference = $parentReference;
$this->name = $name;
}
public function GetURL() {
return $this->GetParentURL() . "/" . $this->name; //Should return "session/login/myvar"
}
private function GetParentURL() {
// Need to reference the class parent here
return $this->parentReference->GetURL();
}
}
$sessionModule = new API_Module("session", "Session");
var_dump($sessionModule->GetMethod("login")->GetVariable("myvar")->GetURL()); //Should return "session/login/myvar"
?>
Now, this works fine, but I'd love to be able to do this without using $parentReference in every single subvariable. It might not be possible, but I'd love to know whether it is or not.
For your example:
$class = new MyClass();
$arrayReference = &$class->array;
GetClassForVariable($arrayReference); //Should return "MyClass"
to find out to which variable originally the alias $arrayReference refers to is not possible in PHP. There is no function available resolving the aliases.
Additionally $class->array is just a variable on it's own. So you would also need to find out based on a value in which class it was defined. That is not possible as well, similar to that PHP does not offer anything to resolve a variable alias, it also does not offer anything to learn about the definition of a variable.
So in short PHP does not have a ReflectionVariable class available ;) I wonder if it is even possible.
The get_class() function should work:
http://php.net/manual/en/function.get-class.php
I agree with GRoNGoR that you shouldn't need to get the parent class of a property of an instantiated object. You could instead just get the name of the class before accessing the property. For example:
$class = new MyClass();
$parent_class = get_class($class); // returns "MyClass"
$arrayReference = &$class->array;
Not sure why you'd need the parent class of the property when you have the object instance and can easily get the parent class from there.

How to dynamically instantiate an object in PHP?

Can we dynamically create and initialize an object in PHP?
This is the normal code:
class MyClass{
var $var1 = null;
var $var2 = null;
.
.
public function __construct($args){
foreach($args as $key => $value)
$this->$key = $value;
}
}
---------------------
$args = ($_SERVER['REQUEST_METHOD'] == "POST") ? $_POST : $_REQUEST;
$obj = new MyClass($args);
The above code works fine. Please note that the names of REQUEST parameters are accurately mapped with the members of class MyClass.
But can we do something like this:
$class = "MyClass";
$obj = new $class;
If we can do like this, then can we initialize $obj by using $args.
According to this post, $obj = $class should work. But it does not work for me. I tried get_class_vars($obj). It threw an exception.
Thanks
It's more a comment, but I leave it here more prominently:
$class = "MyClass";
$obj = new $class($args);
This does work. See newDocs.
You have to overload some other magic methods:
__get (a method that gets called when you call object member)
__set (a method that gets called when you want to set object member)
__isset
__unset
Please see this codepad to see your code rewritten to work with what you want:
<?php
class MyClass{
var $properties = array();
public function __construct($args){
$this->properties = $args;
}
public function __get($name) {
echo "Getting '$name'\n";
if (array_key_exists($name, $this->properties)) {
return $this->properties[$name];
}
return null;
}
}
$args = array("key1" => "value1", "key2" => "value2");
$class = "MyClass";
$obj = new $class($args);
echo "key1:". $obj->key1;
?>
You can use Reflection to instanciate an object with parameters.
<?php
class Foo {
protected $_foo;
protected $_bar;
public function __construct($foo, $bar)
{
$this->_foo = $foo;
$this->_bar = $bar;
}
public function run()
{
echo $this->_foo . ' ' . $this->_bar . PHP_EOL;
}
}
$objectClass = 'Foo';
$args = array('Hello', 'World');
$objectReflection = new ReflectionClass($objectClass);
$object = $objectReflection->newInstanceArgs($args);
$object->run();
See Reflection on php manual.

Categories