Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Two ways of assigning anonymous class to a variable

Below three examples describe anonymous class with very simple and basic but quite understandable example

<?php
// First way - anonymous class assigned directly to variable
$ano_class_obj = new class{
    public $prop1 = 'hello';
    public $prop2 = 754;
    const SETT = 'some config';

    public function getValue()
    {
        // do some operation
        return 'some returned value';
    }

    public function getValueWithArgu($str)
    {
        // do some operation
        return 'returned value is '.$str;
    }
};

echo "
";

var_dump($ano_class_obj);
echo "
";

echo $ano_class_obj->prop1;
echo "
";

echo $ano_class_obj->prop2;
echo "
";

echo $ano_class_obj::SETT;
echo "
";

echo $ano_class_obj->getValue();
echo "
";

echo $ano_class_obj->getValueWithArgu('OOP');
echo "
";

echo "
";

// Second way - anonymous class assigned to variable via defined function
$ano_class_obj_with_func = ano_func();

function ano_func()
{
    return new class {
        public $prop1 = 'hello';
        public $prop2 = 754;
        const SETT = 'some config';

        public function getValue()
        {
            // do some operation
            return 'some returned value';
        }

        public function getValueWithArgu($str)
        {
            // do some operation
            return 'returned value is '.$str;
        }
    };
}

echo "
";

var_dump($ano_class_obj_with_func);
echo "
";

echo $ano_class_obj_with_func->prop1;
echo "
";

echo $ano_class_obj_with_func->prop2;
echo "
";

echo $ano_class_obj_with_func::SETT;
echo "
";

echo $ano_class_obj_with_func->getValue();
echo "
";

echo $ano_class_obj_with_func->getValueWithArgu('OOP');
echo "
";

echo "
";

// Third way - passing argument to anonymous class via constructors
$arg = 1; // we got it by some operation
$config = [2, false]; // we got it by some operation
$ano_class_obj_with_arg = ano_func_with_arg($arg, $config);

function ano_func_with_arg($arg, $config)
{
    return new class($arg, $config) {
        public $prop1 = 'hello';
        public $prop2 = 754;
        public $prop3, $config;
        const SETT = 'some config';

        public function __construct($arg, $config)
        {
            $this->prop3 = $arg;
            $this->config =$config;
        }

        public function getValue()
        {
            // do some operation
            return 'some returned value';
        }

        public function getValueWithArgu($str)
        {
            // do some operation
            return 'returned value is '.$str;
        }
    };
}

echo "
";

var_dump($ano_class_obj_with_arg);
echo "
";

echo $ano_class_obj_with_arg->prop1;
echo "
";

echo $ano_class_obj_with_arg->prop2;
echo "
";

echo $ano_class_obj_with_arg::SETT;
echo "
";

echo $ano_class_obj_with_arg->getValue();
echo "
";

echo $ano_class_obj_with_arg->getValueWithArgu('OOP');
echo "
";

echo "
";
Comment

PREVIOUS NEXT
Code Example
Php :: wpmu assign user to blog 
Php :: codeingniter 3 not like 
Php :: show dot dot after some words php 
Php :: GZIP COMPRESSION With PHP 
Php :: wp cpt dashicon 
Php :: how to override category product from seo title and description 
Php :: php foreach show only 4 
Php :: learnpress wordpress plugin shortcode 
Php :: child data retrive without timestamp laravel 
Php :: checnge message no products were found matching your selection woocommerce edit 
Php :: live search in ajax 
Php :: php int to indonesian rupiah 
Php :: how to upload images to sql database php PDO 
Php :: php array push key value 
Php :: laravel 
Php :: html table to array php 
Php :: laravel data type 
Php :: how to append one array to another array in php 
Php :: Clear Caching of Queries Laravel Specific Model Cache 
Php :: recorrer un array php 
Php :: phpunit check exception not thrown 
Java :: Cannot fit requested classes in a single dex file 
Java :: how to get current date time in android 
Java :: rgb to hex java 
Java :: java execution time 
Java :: java how to print an array 
Java :: java random boolean 
Java :: cast long to string java 
Java :: java print treemap 
Java :: java bufferedimage get int array 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =