Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Creating PHP class instance from string

// Way #1
$className = "AppMyClass";
$instance = new $className();

// ----------------------

// Way #2
$className = "AppMyClass";
$class = new ReflectionClass($className);

// Create a new Instance without arguments:
$instance = $class->newInstance();

// Create a new Instance with arguments (need a contructor):
$instance = $class->newInstanceArgs(["Banana", "Apple"]);
Comment

php instance class from string

$class = 'FooBarMyClass'; 
$instance = new $class();
Comment

php class instance

<?php
// Create the class first...
class Person
{
    public $name;
    public $surname;
    
    function __construct($name, $surname)
    {
    	$this->name = $name;
        $this->surname = $surname;
    }
    
    function getName()
    {
    	return $this->name;
    }
    
    function getSurname()
    {
    	return $this->surname;
    }
    
    function setName($value)
    {
    	$this->name = $value;
    }
    
    function setSurname($value)
    {
    	$this->surname = $value;
    }
}

// Now, you can create an instance of the class
$me = New Person("John", "Doe");
echo $me->getName();
$me.setName("Jane");
echo $me->getName();

/**
   I hope that this helps ^_^ 
 **/
?>
Comment

PREVIOUS NEXT
Code Example
Php :: php $randomUA[rand(0, count($randomUA) 1) 
Php :: laravel check record exists 
Php :: how to generate random string in laravel 
Php :: how send parameter with command in laravel 
Php :: laravel 8 date format 
Php :: php shell script 
Php :: add foreign key column laravel 5.8 
Php :: wordpress get site title 
Php :: php mkdir 
Php :: php closecursor 
Php :: continue php 
Php :: wp get author description 
Php :: laravel log daily 
Php :: php case switch 
Php :: laravel run seeder 
Php :: PHP Startup: Unable to load dynamic library 
Php :: php random string 
Php :: php json encode 
Php :: laravel find by 
Php :: strval in php 
Php :: where not null in laravel 
Php :: yii app db createcommand join yii1 
Php :: get current url in controller in laravel 
Php :: https redirect in htacess for php laravel 
Php :: upload webp to wordpress 
Php :: sum of columns laravel eloquent 
Php :: php serialize array 
Php :: How to get a WordPress post by slug 
Php :: add log in laravel 8 
Php :: percentage in php 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =