pTutorial

New way to learn Programming

PHP 5 __set Method

PHP __set Magic Methods

PHP is a loosely coupled programming language, so that you can use variable without declaring it. It is one of the features of a loosely coupled programming language.

PHP __set method is called whenever write to an undefined property of the object.

you create a method called _set() within your Class to give the response of undefined property and its value.

_set() method needs two argument the name of the property and its value to set it to. It does not need to return a value:

Example

<?php

class Computer 
{

public function __set( $model, $Value )
{
echo "The requested $model and the property $Value 
"; } } $Computer = new Computer; $a = $Computer-> mode="Sony e series"; ?>

Output

The requested mode and the property Sony e series

If you run this code it will run properly even assign a value to undefined variable "mode".


QUOTES OF THE DAY

Eat to live, and not live for eat.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER