A method for initializing properties in a class. PHP code ex... open

A method for initializing properties in a class. PHP code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

This code defines a PHP class called Example_Class which has several properties including $place, $callback, and $args.

It also defines a constructor method that initializes the properties of the object using the init_properties() method. The init_properties() method takes an array of arguments and assigns their values to the corresponding class properties.

class Example_Class {

	public $place;
	public $callback;
	public $args = array(
		'id'      => '',
		'class'   => '',
		'title'   => '',
		'gallery' => 0,
		'public'  => 1,
		'order'   => 10,
		'width'   => 40
	);

	function __construct( $data ) {
		$this->init_properties( $data );
	}

	function init_properties( $args ) {

		$properties = get_class_vars( get_class( $this ) );

		foreach ( $properties as $name => $val ) {

			if ( ! isset( $args[ $name ] ) ) {
				continue;
			}

			$this->$name = $args[ $name ];
		}
	}
}
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation