Saturday, December 21, 2013

What is Reflection in php and why it is use in real life oops

Reflection usually refers to accessing the definition of types and functions during runtime. It is most useful in static languages such as C# or Java, but I guess it could be useful in dynamic languages such as PHP too.

In PHP, it seems it can be used to retrieve documentation of existing classes/functions. But I don't think it will help you with writing documentation.
Reflection can be used for observing and/or modifying program execution at runtime. A reflection-oriented program component can monitor the execution of an enclosure of code and can modify itself according to a desired goal related to that enclosure. This is typically accomplished by dynamically assigning program code at runtime.
In object oriented programming languages such as Java, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.

Reflection can also be used to adapt a given program to different situations dynamically. For example, consider an application that uses two different classes X and Yinterchangeably to perform similar operations. Without reflection-oriented programming, the application might be hard-coded to call method names of class X and class Y. However, using the reflection-oriented programming paradigm, the application could be designed and written to utilize reflection in order to invoke methods in classes X and Y without hard-coding method names. Reflection-oriented programming almost always requires additional knowledge, framework, relational mapping, and object relevance in order to take advantage of more generic code execution. Hard-coding can be avoided to the extent that reflection-oriented programming is used.

Reflection is often used as part of software testing, such as for the runtime creation/instantiation of mock objects.

Reflection is also a key strategy for metaprogramming.
The name reflection is used to describe code which is able to inspect other code in the same system (or itself).
For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.
So, to give you a code example of this in Java (imagine the object in question is foo) :
Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);
One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.
There are some good reflection examples to get you started athttp://docs.oracle.com/javase/tutorial/reflect/index.html
And finally, yes, the concepts are pretty much similar in other statically types languages which support reflection (like C#). In dynamically typed languages, the use case described above is less necessary (since the compiler will allow any method to be called on any object, failing at runtime if it does not exist), but the second case of looking for methods which are marked or work in a certain way is still common.
This group of classes was created for the express purpose of introspecting other classes. These classes make it possible to examine the properties of other classes by retrieving metadata about classes; you can even use them to examine the reflection classes themselves.
Reflection provides information about the modifiers of a class or interface—whether that class is final or static, for example.
It can also reveal all the methods and data members of a class and all the modifiers applied to them.
Parameters passed to methods can also be introspected and the names of variables exposed. Through reflection it is possible to automate the documentation of built-in classes or user-defined classes.
It turns out that the central repository of information about classes was right in front of us all the time. PHP can tell us all about itself through the mirror of the reflection classes.
The reflection group of classes or Application Programming Interface (API) is made up of a number of different classes and one
<?php
class HelloBikash{

    public function sayHelloTo($name) {
        return 'Hello ' . $name;
    }

}

$reflectionMethod = new ReflectionMethod('HelloBikash', 'sayHelloTo');
echo $reflectionMethod->invoke(new HelloBikash(), 'Mike');
?>
Reflection class are

Thursday, December 19, 2013

how to do replication using my mysql database my.ini configuration setup

As we know that MySQL replication works with binary log. Master create logs of the queries which made modification in database. And then master send event to the slave to execute that query. So on master server we need to setup binary log and also we can inform master server to create log for which database(the databases which we want to replicate).
In my case I have added following line in my master server my.ini file to start binary logging for database replication_test.
create log-bin directory if folder not available 
log-bin="C:/xampp/mysql/data/binlog/bin-log" #Specifing path to store binary log
log-bin-index=c:/xampp/mysql/data/bin-log-index #specify path to store binary log indexes
binlog-do-db=replication_test #Database for which binary log is enable.
server-id=1 #Server ID which is mandatory for replication

After specifying above setting in your master server please restart mysql service on your master server.
After restarting your master server please hit following query on your master :
show master status
If your query is giving following result then every thing is fine:
Master Replication StatusNow let us create a user on master server which will be used for the replication purpose. All you need to do here is to create a user and grant replication slave privileges to that user. You can do it using following query on master server
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'replication';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';

Now almost you are done with the setting which is required on master server. Below steps are now for the setting of slave server. Let us jump to another server now.
Slave Server Setup: On slave first you need to mention your master server details in your my.cnf/my.ini
server-id=2 #Unique number like primary server
master-host=192.168.43.210 #IP of master server
master-user=replication_user #User we have created for the replication on slave
master-password=replication #Password of the userver
master-connect-retry=60   #retry time
replicate-do-db=replication_test #name of the database to be replicated.
Now after adding this configuration please make sure that your both server(master and slave) has same data for the database which you are going to replicate(in our case replication_test database). If your both server has identical data then you can restart your slave server after adding above configuration. Otherwise first make both master and slave identical and then restart your slave server.Now hit the following query on slave server:
SHOW SLAVE STATUS;
And if you are getting following result then every thing is fine and you did it:
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master |
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Waiting for master to send event | 192.168.43.210 | replication_user | 3306 | 60 | bin-log.000043 | 106 | mysqld-relay-bin.000002 | 241 | bin-log.000043 | Yes | Yes | replication_test | | | | | | 0 | | 0 | 106 | 241 | None | | 0 | No | | | | | | 0 |
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
To check the replication let us create a table on master server and see what is happening on slave server. I have used following query to create table on master server:
CREATE TABLE `replication_test`.`test`( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255), PRIMARY KEY (`id`) );
Now I can see the same table created in the slave replication_test database. WOW!!!!!