Hello All

Today I will focus on overloading method and property in PHP. Unlike other language Java/C# overloading is working in a little bit different way for PHP.

Normally we can overload a method by providing different/varying argument(s) ,but in PHP it should have to create through some magic methods.

These magic methods are as bellow:

(For overloading property)

void __set ( string $name , mixed $value )

mixed __get ( string $name )

bool __isset ( string $name )

void __unset ( string $name )

(For overloading method)

mixed __call ( string $name , array $arguments )

mixed __callStatic ( string $name , array $arguments )

It will be better to describe the operation inside code comment rather that bookish thing hear. Please go through the code comment to get know what is gonging over there to perform overloading .

<?php

class PhpOverloading {

  /* Property overloading -start- */
  private $_data = array ();

  public function __set($name, $value) {
    /*
     * set the key value in the $_data private
     * variable.
     * */
    $this->_data [$name] = $value;
  }

  public function __get($name) {
    /*
     * Retrieve the value of key which we have
     * set through __set method.
     * */
    return $this->_data [$name];
  }

  public function __isset($name) {
    /*
     * Check wheather the key exist or not.
     * */
    return isset ( $this->_data [$name] );
  }

  public function __unset($name) {
    /*
     * Unset the value of key.
     * */
    unset ( $this->_data [$name] );
  }

  /* Property overloading -end- */

  /* Method overloading -start- */
  public function __call($name, $argument) {
    /*
     * perform your operation as needed,
     * I just used a switch case to show how
     * we can set the dynamic method with parameters.
     * */

    switch ($name) {
      case ('First') :
        echo "This is the first dyanamic method and arguments are " . implode ( ',', $argument );
        break;
      case ('Second') :
        echo "2nd method and the arguments are {$argument[1]} and {$argument[2]}";
        break;
      default :
        echo "Method unhandled with the name {$name} and arguments are " . implode ( ',', $argument );
    }
  }

  public static function __callstatic($name, $argument) {

    /*
     * It's similar as __call but works as static method
     * call.
     * */
    switch ($name) {
      case ('FirstStatic') :
        echo "Static first dyanamic method and arguments are " . implode ( ',', $argument );
        break;
      case ('SecondStatic') :
        echo "Static 2nd method and the arguments are {$argument[1]} and {$argument[2]}";
        break;
      default :
        echo "Static method unhandled with the name {$name} and arguments are " . implode ( ',', $argument );
    }
  }

/* Method overloading -end- */
}

$phpOverloading = new PhpOverloading ();

echo 'Property overloading output -start-';
echo "<br /><br />";
var_dump ( isset ( $phpOverloading->dynamicProperty ) );
$phpOverloading->dynamicProperty = "new property injected";

echo "<br />";
echo $phpOverloading->dynamicProperty;
echo "<br />";

var_dump ( isset ( $phpOverloading->dynamicProperty ) );

unset ( $phpOverloading->dynamicProperty );

echo "<br />";
var_dump ( isset ( $phpOverloading->dynamicProperty ) );
echo "<br /><br />";

echo 'Property overloading output -end-';

echo "<br /><br />";

echo 'Method overloading output -start-';

echo '<br /><br />';
$phpOverloading->First ( 'one', 'two', 'three' );
echo "<br />";
$phpOverloading->Second ( '1', '2' );
echo "<br />";
$phpOverloading->UnHandledMethod ( '1', '2' );
echo "<br /><br />";

echo 'staic method overloading';
echo "<br /><br />";
PhpOverloading::FirstStatic ( 'one', 'two', 'three' );
echo "<br />";
PhpOverloading::SecondStatic ( '1', '2' );
echo "<br />";
PhpOverloading::UnHandledStaticMethod ( '1', '2' );
echo "<br /><br />";

echo 'Method overloading output -end-';

/*
 * final output is as bellow:

Property overloading output -start-

bool(false)
new property injected
bool(true)
bool(false)

Property overloading output -end-

Method overloading output -start-

This is the first dyanamic method and arguments are one,two,three
2nd method and the arguments are 2 and
Method unhandled with the name UnHandledMethod and arguments are 1,2

staic method overloading

Static first dyanamic method and arguments are one,two,three
Static 2nd method and the arguments are 2 and
Static method unhandled with the name UnHandledStaticMethod and arguments are 1,2

Method overloading output -end-

 *
 * */

You may check my other posts from my [ Blog ] .

Please provide your feedback.

Thanks

That’s all for today.

BYE

Hello All

Today I will focus on overloading method and property in PHP. Unlike other language Java/C# overloading is working in a little bit different way for PHP.

Normally we can overload a method by providing different/varying argument(s) ,but in PHP it should have to create through some magic methods.

These magic methods are as bellow:

(For overloading property)

void __set ( string $name , mixed $value )

mixed __get ( string $name )

bool __isset ( string $name )

void __unset ( string $name )

(For overloading method)

mixed __call ( string $name , array $arguments )

mixed __callStatic ( string $name , array $arguments )

It will be better to describe the operation inside code comment rather that bookish thing hear. Please go through the code comment to get know what is gonging over there to perform overloading .

<?php

class PhpOverloading {

  /* Property overloading -start- */
  private $_data = array ();

  public function __set($name, $value) {
    /*
     * set the key value in the $_data private
     * variable.
     * */
    $this->_data [$name] = $value;
  }

  public function __get($name) {
    /*
     * Retrieve the value of key which we have
     * set through __set method.
     * */
    return $this->_data [$name];
  }

  public function __isset($name) {
    /*
     * Check wheather the key exist or not.
     * */
    return isset ( $this->_data [$name] );
  }

  public function __unset($name) {
    /*
     * Unset the value of key.
     * */
    unset ( $this->_data [$name] );
  }

  /* Property overloading -end- */

  /* Method overloading -start- */
  public function __call($name, $argument) {
    /*
     * perform your operation as needed,
     * I just used a switch case to show how
     * we can set the dynamic method with parameters.
     * */

    switch ($name) {
      case ('First') :
        echo "This is the first dyanamic method and arguments are " . implode ( ',', $argument );
        break;
      case ('Second') :
        echo "2nd method and the arguments are {$argument[1]} and {$argument[2]}";
        break;
      default :
        echo "Method unhandled with the name {$name} and arguments are " . implode ( ',', $argument );
    }
  }

  public static function __callstatic($name, $argument) {

    /*
     * It's similar as __call but works as static method
     * call.
     * */
    switch ($name) {
      case ('FirstStatic') :
        echo "Static first dyanamic method and arguments are " . implode ( ',', $argument );
        break;
      case ('SecondStatic') :
        echo "Static 2nd method and the arguments are {$argument[1]} and {$argument[2]}";
        break;
      default :
        echo "Static method unhandled with the name {$name} and arguments are " . implode ( ',', $argument );
    }
  }

/* Method overloading -end- */
}

$phpOverloading = new PhpOverloading ();

echo 'Property overloading output -start-';
echo "<br /><br />";
var_dump ( isset ( $phpOverloading->dynamicProperty ) );
$phpOverloading->dynamicProperty = "new property injected";

echo "<br />";
echo $phpOverloading->dynamicProperty;
echo "<br />";

var_dump ( isset ( $phpOverloading->dynamicProperty ) );

unset ( $phpOverloading->dynamicProperty );

echo "<br />";
var_dump ( isset ( $phpOverloading->dynamicProperty ) );
echo "<br /><br />";

echo 'Property overloading output -end-';

echo "<br /><br />";

echo 'Method overloading output -start-';

echo '<br /><br />';
$phpOverloading->First ( 'one', 'two', 'three' );
echo "<br />";
$phpOverloading->Second ( '1', '2' );
echo "<br />";
$phpOverloading->UnHandledMethod ( '1', '2' );
echo "<br /><br />";

echo 'staic method overloading';
echo "<br /><br />";
PhpOverloading::FirstStatic ( 'one', 'two', 'three' );
echo "<br />";
PhpOverloading::SecondStatic ( '1', '2' );
echo "<br />";
PhpOverloading::UnHandledStaticMethod ( '1', '2' );
echo "<br /><br />";

echo 'Method overloading output -end-';

/*
 * final output is as bellow:

Property overloading output -start-

bool(false)
new property injected
bool(true)
bool(false)

Property overloading output -end-

Method overloading output -start-

This is the first dyanamic method and arguments are one,two,three
2nd method and the arguments are 2 and
Method unhandled with the name UnHandledMethod and arguments are 1,2

staic method overloading

Static first dyanamic method and arguments are one,two,three
Static 2nd method and the arguments are 2 and
Static method unhandled with the name UnHandledStaticMethod and arguments are 1,2

Method overloading output -end-

 *
 * */

You may check my other posts from my [ Blog ]

Please provide your feedback.

Thanks

That’s all for today.

BYE

Hello All

Few days back I was struggling with a repeater problem , where I have to get a value of dropdown list on a button click event which is inside repeater. Eventually I come up with a solution through googling. What I have faced was get the value of appropriate dropdown list while clicking the button of a particular repeater’s row.

It’s being hard to describe rather that visualize :) isn’t it ! ( proposed solution should be as image bellow)

    My aspx markup is as bellow :

 <asp:Repeater runat="server" ID="rptTeamInfo" OnItemDataBound="rptTeamInfo_ItemDataBound" OnItemCommand="rptTeamInfo_ItemCommand">
               <ItemTemplate>
                <div class="side2">
                <div class="form">
                    <a href="#">delete</a><a href="#">edit /</a>
                    <ul class="p2">
                      <span class="style1">[ <%#Eval("team_name")%> ]</span> - [ <%#Eval("creation_date")%>]<br />
                      <span class="style2">[<asp:Label runat="server" Text="ADMIN bla bla bla"></asp:Label>] - [ <%#Eval("team_leader")%>]</span> - [ <%#Eval("description")%>]
                     </ul>
                </div>
                <div class="form2">
                <asp:ImageButton id="imgAddToTeam"  CommandName="select" CommandArgument='<%# Eval("team_id") %>'  ImageUrl="images/Add_to_team.png" runat="server" style="margin:10px;" align="right"></asp:ImageButton>
                <asp:DropDownList ID="ddlUserList" runat="server">

                </asp:DropDownList>

                </div>
                <div class="row1">
                        <ul class="column">
                            <p><img src="images/name.png" /> Name</p>
                        </ul>
                        <ul class="column2">
                            <p><img src="images/username.png" /> Username</p>
                        </ul>
                        <ul class="column3">
                            <p><img src="images/username.png" /> Access</p>
                        </ul>
                </div>
                <div class="row2">
                 <asp:Repeater ID="rptUserInfo"  OnItemDataBound="rptUserInfo_ItemDataBound" runat="server">
                       <ItemTemplate>
                    <asp:LinkButton id="lnkUserDelete" onclick="lnkUserDelete_Click" runat="server">delete</asp:LinkButton>
                    <ul class="column">
                        <p> <asp:Label runat="server" id="lblFirstNameIn"></asp:Label> <asp:Label runat="server" id="lblLastNameIn"></asp:Label></p>
                    </ul>
                    <ul class="column2">
                        <p> <asp:Label runat="server" id="lblUserIDIn"></asp:Label></p>
                    </ul>
                    <ul class="column3">
                        <p><asp:Label runat="server" id="lblUserTypeIn"></asp:Label></p>
                    </ul>
                        </ItemTemplate>
                 </asp:Repeater>
                </div>
            </div>
               </ItemTemplate>
               </asp:Repeater>

And my code behind file is as bellow :

using System;
using System.Web.UI.WebControls;
using ClassLibrary1.UTILITY;

namespace WebDoc
{
    public partial class ManageTeam : System.Web.UI.Page
    {
        public int CurrentTeamID { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblUserId.Text = BUSessionUtility.BUSessionContainer.UserName;
            ManageTeamDAL manageTeamDAL = new ManageTeamDAL();
            rptTeamInfo.DataSource = manageTeamDAL.GetGroupsByManagerID(BUSessionUtility.BUSessionContainer.UserName);

            rptTeamInfo.DataBind();

        }
        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            BUSessionUtility.BUSessionContainer.UserName = string.Empty;
            BUSessionUtility.BUSessionContainer.UserType = string.Empty;
            Response.Redirect("~/LoginUI.aspx");
        }

        protected void rptTeamInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DropDownList ddlUserList = (DropDownList)(e.Item.FindControl("ddlUserList"));

            ManageTeamDAL manageTeamDAL = new ManageTeamDAL();
            ddlUserList.DataSource = manageTeamDAL.GetUsersByManagerID(BUSessionUtility.BUSessionContainer.UserName);
            ddlUserList.DataTextField = "id";
            ddlUserList.DataValueField = "id";
            ddlUserList.DataBind();

            Repeater rptUserInfo = (Repeater)(e.Item.FindControl("rptUserInfo"));

            var row = e.Item.DataItem;
            rptUserInfo.DataSource = manageTeamDAL.GetUsersByAssignedTeamID(((team_info)(row)).team_id);
            rptUserInfo.DataBind();

        }

        protected void rptTeamInfo_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "select")
            {
                ImageButton imgAddToTeam = (ImageButton)e.CommandSource;

                DropDownList ddlUserList = (DropDownList)rptTeamInfo.Items[e.Item.ItemIndex].FindControl("ddlUserList");

                ManageTeamDAL manageTeamDAL = new ManageTeamDAL();

                manageTeamDAL.AssignUserInTeam(int.Parse(imgAddToTeam.CommandArgument), ddlUserList.SelectedValue.ToString());

                rptTeamInfo.DataSource = manageTeamDAL.GetGroupsByManagerID(BUSessionUtility.BUSessionContainer.UserName);

                rptTeamInfo.DataBind();
            }
        }

        protected void rptUserInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblFirstName = (Label)(e.Item.FindControl("lblFirstNameIn"));
            Label lblLastName = (Label)(e.Item.FindControl("lblLastNameIn"));
            Label lblUserID = (Label)(e.Item.FindControl("lblUserIDIn"));
            Label lblUserType = (Label)(e.Item.FindControl("lblUserTypeIn"));
            LinkButton lnkUserDelete = (LinkButton)(e.Item.FindControl("lnkUserDelete"));

            var row = e.Item.DataItem;

            lblFirstName.Text = ((user_info)(row)).f_name;
            lblLastName.Text = ((user_info)(row)).l_name;
            lblUserID.Text = ((user_info)(row)).id;
            lblUserType.Text = ((user_info)(row)).user_type;
            lnkUserDelete.CommandArgument = ((user_info)(row)).id;

        }

        protected void lnkUserDelete_Click(object sender, EventArgs e)
        {
            ManageTeamDAL manageTeamDAL = new ManageTeamDAL();

            manageTeamDAL.DeleteUserformTeam(((LinkButton)(sender)).CommandArgument);

            rptTeamInfo.DataSource = manageTeamDAL.GetGroupsByManagerID(BUSessionUtility.BUSessionContainer.UserName);

            rptTeamInfo.DataBind();
        }

    }
}

Not all codes are important here and I will escape the data binding part as its not the focus of today’s topic. Now I will focus what we should do to perform our basic task.

first of all we have to do one most important  thing *** MAKE    EnableViewState="false" *** If ViewState is in true condition your are not going to handle  OnItemCommand event which will significantly react for your particular row’s activity.

 <asp:Repeater runat="server" ID="rptTeamInfo" OnItemDataBound="rptTeamInfo_ItemDataBound" OnItemCommand="rptTeamInfo_ItemCommand">

<ItemTemplate>

<asp:ImageButton id="imgAddToTeam" CommandName="select" CommandArgument=’<%# Eval("team_id") %>ImageUrl="images/Add_to_team.png" runat="server" style="margin:10px;" align="right"></asp:ImageButton>

<asp:DropDownList ID="ddlUserList" runat="server">

</asp:DropDownList>

</ItemTemplate>

</asp:Repeater>

The event handler “rptTeamInfo_ItemCommand” for the the event “OnItemCommand” is as bellow.

protected void rptTeamInfo_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "select")
    {
        ImageButton imgAddToTeam = (ImageButton)e.CommandSource;

        DropDownList ddlUserList = (DropDownList)rptTeamInfo.Items[e.Item.ItemIndex].FindControl("ddlUserList");

        ManageTeamDAL manageTeamDAL = new ManageTeamDAL();

        manageTeamDAL.AssignUserInTeam( int.Parse(imgAddToTeam.CommandArgument),ddlUserList.SelectedValue.ToString() );

        rptTeamInfo.DataSource = manageTeamDAL.GetGroupsByManagerID(BUSessionUtility.BUSessionContainer.UserName);

        rptTeamInfo.DataBind();
    }
}

We are passing the “CommandName” and “CommandArgument” through our image button which will check the command name in code behind and act with the command argument parameter.

We can get our ImageButton and DropdownList is as bellow from our code behind.

ImageButton imgAddToTeam = (ImageButton)e.CommandSource;

DropDownList ddlUserList = (DropDownList)rptTeamInfo.Items[e.Item.ItemIndex].FindControl("ddlUserList");

And selet the value of dropdown as bellow.

ddlUserList.SelectedValue

In this way we can accomplish our task regarding the repeater row’s control manipulation.

Thats all for today.

BYE