add a note

User Contributed Notes 9 notes

up
66
Jason
7 years ago
For real quick and dirty one-liner anonymous objects, just cast an associative array:

<?php

$obj
= (object) array('foo' => 'bar', 'property' => 'value');

echo
$obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'

?>

... no need to create a new class or function to accomplish it.
up
34
farzan at ifarzan dot com
11 years ago
PHP 5 is very very flexible in accessing member variables and member functions. These access methods maybe look unusual and unnecessary at first glance; but they are very useful sometimes; specially when you work with SimpleXML classes and objects. I have posted a similar comment in SimpleXML function reference section, but this one is more comprehensive.

I use the following class as reference for all examples:

<?php
class Foo {
    public
$aMemberVar = 'aMemberVar Member Variable';
    public
$aFuncName = 'aMemberFunc';
   
   
    function
aMemberFunc() {
        print
'Inside `aMemberFunc()`';
    }
}

$foo = new Foo;
?>

You can access member variables in an object using another variable as name:

<?php
$element
= 'aMemberVar';
print
$foo->$element; // prints "aMemberVar Member Variable"
?>

or use functions:

<?php
function getVarName()
{ return
'aMemberVar'; }

print
$foo->{getVarName()}; // prints "aMemberVar Member Variable"
?>

Important Note: You must surround function name with { and } or PHP would think you are calling a member function of object "foo".

you can use a constant or literal as well:

<?php
define
(MY_CONSTANT, 'aMemberVar');
print
$foo->{MY_CONSTANT}; // Prints "aMemberVar Member Variable"
print $foo->{'aMemberVar'}; // Prints "aMemberVar Member Variable"
?>

You can use members of other objects as well:

<?php
print $foo->{$otherObj->var};
print
$foo->{$otherObj->func()};
?>

You can use mathods above to access member functions as well:

<?php
print $foo->{'aMemberFunc'}(); // Prints "Inside `aMemberFunc()`"
print $foo->{$foo->aFuncName}(); // Prints "Inside `aMemberFunc()`"
?>
up
-21
midir
7 years ago
There are a couple of tricks you can do with PHP's classes that programmers from C++, etc., will find very peculiar, but which can be useful.

You can create instances of classes without knowing the class name in advance, when it's in a variable:

<?php

$type
= 'cc';
$obj = new $type; // outputs "hi!"

class cc {
    function
__construct() {
        echo
'hi!';
    }
}

?>

You can also conditionally define them by wrapping them in if/else blocks etc, like so:

<?php

if (expr) {
    class
cc {
       
// version 1
   
}
} else {
    class
cc {
       
// version 2
   
}
}

?>

It makes up for PHP's lack of preprocessor directives. The caveat is that the if/else code body must have been executed before you can use the class, so you need to pay attention to the order of the code, and not use things before they're defined.
up
-24
dances_with_peons at live dot com
5 years ago
As of PHP 5.3, $className::funcName() works fine.

<?php

 
class test
 
{
    public static function
run() { print "Works\n"; }
  }

 
$className = 'test';
 
$className::run();

?>

on my system, prints "Works".  May work with earlier versions of PHP as well.  Even if it doesn't, there's always

<?php

  $className
= 'test';
 
call_user_func(array($className, 'run'));

?>

The point is, there's no need for eval.
up
-28
corpus-deus at softhome dot net
5 years ago
With regards to Singleton patterns (and variable class names) - try:

<?php
class MyClass {

 
// singleton instance
 
private static $instance;

 
// private constructor function
  // to prevent external instantiation
 
private __construct() { }

 
// getInstance method
 
public static function getInstance() {

    if(!
self::$instance) {
     
self::$instance = new self();
    }

    return
self::$instance;

  }

 
//...

}
?>
up
-27
Ashley Dambra
2 years ago
Here a simple class 'stdObject' that give us the possibility to create dynamic classes and the possibility to add and execute methods thing that 'stdClass' don't let us do.  Very useful if you extends it to a controller on MVC Design pattern. Let users create own classes.

I have also post this class on http://www.php.net/manual/en/language.types.object.php

<?php
class stdObject {
    public function
__construct(array $arguments = array()) {
        if (!empty(
$arguments)) {
            foreach (
$arguments as $property => $argument) {
               
$this->{$property} = $argument;
            }
        }
    }

    public function
__call($method, $arguments) {
       
$arguments = array_merge(array("stdObject" => $this), $arguments); // Note: method argument 0 will always referred to the main class ($this).
       
if (isset($this->{$method}) && is_callable($this->{$method})) {
            return
call_user_func_array($this->{$method}, $arguments);
        } else {
            throw new
Exception("Fatal error: Call to undefined method stdObject::{$method}()");
        }
    }
}

// Usage.

$obj = new stdObject();
$obj->name = "Nick";
$obj->surname = "Doe";
$obj->age = 20;
$obj->adresse = null;

$obj->getInfo = function($stdObject) { // $stdObject referred to this object (stdObject).
   
echo $stdObject->name . " " . $stdObject->surname . " have " . $stdObject->age . " yrs old. And live in " . $stdObject->adresse;
};

$func = "setAge";
$obj->{$func} = function($stdObject, $age) { // $age is the first parameter passed when calling this method.
   
$stdObject->age = $age;
};

$obj->setAge(24); // Parameter value 24 is passing to the $age argument in method 'setAge()'.

// Create dynamic method. Here i'm generating getter and setter dynimically
// Beware: Method name are case sensitive.
foreach ($obj as $func_name => $value) {
    if (!
$value instanceOf Closure) {

       
$obj->{"set" . ucfirst($func_name)} = function($stdObject, $value) use ($func_name) {  // Note: you can also use keyword 'use' to bind parent variables.
           
$stdObject->{$func_name} = $value;
        };

       
$obj->{"get" . ucfirst($func_name)} = function($stdObject) use ($func_name) {  // Note: you can also use keyword 'use' to bind parent variables.
           
return $stdObject->{$func_name};
        };

    }
}

$obj->setName("John");
$obj->setAdresse("Boston");

$obj->getInfo();
?>
up
-33
redrik at gmail dot com
7 years ago
Maybe someone will find these classes, which simulate enumeration, useful.
<?php
class Enum {
    protected
$self = array();
    public function
__construct( /*...*/ ) {
       
$args = func_get_args();
        for(
$i=0, $n=count($args); $i<$n; $i++ )
           
$this->add($args[$i]);
    }
   
    public function
__get( /*string*/ $name = null ) {
        return
$this->self[$name];
    }
   
    public function
add( /*string*/ $name = null, /*int*/ $enum = null ) {
        if( isset(
$enum) )
           
$this->self[$name] = $enum;
        else
           
$this->self[$name] = end($this->self) + 1;
    }
}

class
DefinedEnum extends Enum {
    public function
__construct( /*array*/ $itms ) {
        foreach(
$itms as $name => $enum )
           
$this->add($name, $enum);
    }
}

class
FlagsEnum extends Enum {
    public function
__construct( /*...*/ ) {
       
$args = func_get_args();
        for(
$i=0, $n=count($args), $f=0x1; $i<$n; $i++, $f *= 0x2 )
           
$this->add($args[$i], $f);
    }
}
?>
Example usage:
<?php
$eFruits
= new Enum("APPLE", "ORANGE", "PEACH");
echo
$eFruits->APPLE . ",";
echo
$eFruits->ORANGE . ",";
echo
$eFruits->PEACH . "\n";

$eBeers = new DefinedEnum("GUINESS" => 25, "MIRROR_POND" => 49);
echo
$eBeers->GUINESS . ",";
echo
$eBeers->MIRROR_POND . "\n";

$eFlags = new FlagsEnum("HAS_ADMIN", "HAS_SUPER", "HAS_POWER", "HAS_GUEST");
echo
$eFlags->HAS_ADMIN . ",";
echo
$eFlags->HAS_SUPER . ",";
echo
$eFlags->HAS_POWER . ",";
echo
$eFlags->HAS_GUEST . "\n";
?>
Will output:
1, 2, 3
25, 49
1,2,4,8 (or 1, 10, 100, 1000 in binary)
up
-33
0x174[d0t]14[at]gmail[d0t]com
1 year ago
A little trick for create an anonymous object  (exactly an half-anonymous object :D aka "_") who can handle methods.

<?php

class _
{
    public function
__construct( array $cfg){
        foreach(
$cfg as $k=>$v){
           
$this->{$k}=$v;
        }
    }
   
    public function
__call( $fn, array $args){
        if(isset(
$this->{$fn})){
           
array_unshift($args, $this);
           
call_user_func_array( $this->{$fn}, $args);
        }
    }
}

$o = new _(array(
   
"color"=>"red",
   
"run"=>function($this){
        echo
"My color is : ".$this->color;
    }
));

$o->run();

?>
up
-7
colonel sanders
1 month ago
"mathod" misspelling

if u care

is
this
long
enough
yet