Showing posts with label PHP OOPS CONCEPTS. Show all posts
Showing posts with label PHP OOPS CONCEPTS. Show all posts

Friday, 19 October 2012

How to use commit and rollback transactions using symfony?

Hi All,

$con = Doctrine_Manager::connection();
try {
    $con->beginTransaction();
    // execute all queries...
    $con->commit();
   
}catch (Exception $e) {
   
        $con->rollback();       
        // error message will come here...
   

}

Cheers!

Tuesday, 15 May 2012

How to implement facebook connect using facebook javascript?

Guys,


######### HTML PART #############
<!---fb:login scope="email" >Login with Facebook</fb:login-->
<input type="button" id="login_button_server" onclick="facebookLogin()" value="Facebook Login"/>
<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId: '7845787548501',
            cookie: true,
            xfbml: true,
            oauth: true
        });
        FB.Event.subscribe('auth.statusChange', function(response) {
            //alert(response.status);
            if (response.status === 'connected') {

                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                FB.api('/me', function(response) {

                    //alert(response);
                    var email = response.email;
                    var name = response.name;
                    var username = response.username;
                    alert('Good to see you, ' + response.email + '.');
                });
            }else{
                alert("Error found while logged in. Please try again.");
            }
        });

    };
    (function () {       
        var e = document.createElement('script');
        e.async = true;
        e.type = 'text/javascript';
        e.src = 'https://connect.facebook.net/en_US/all.js';       
        document.getElementsByTagName('head')[0].appendChild(e);
    }());


    function facebookLogin() {
        FB.login(function(response) {}, {scope:'email'});
    }
</script>

Cheers!

Thursday, 20 May 2010

What are Constructors and Destructors?

Constructor

void __construct ([ mixed $args [, $... ]] )
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. 

<?phpclass BaseClass {
   function 
__construct() {
       print 
"In BaseClass constructor\n";
   }
}

class 
SubClass extends BaseClass {
   function 
__construct() {
       
parent::__construct();
       print 
"In SubClass constructor\n";
   }
}
$obj = new BaseClass();$obj = new SubClass();?>
 
 

Destructor

void __destruct ( void )
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. 


<?phpclass MyDestructableClass {
   function 
__construct() {
       print 
"In constructor\n";
       
$this->name = "MyDestructableClass";
   }

   function 
__destruct() {
       print 
"Destroying " . $this->name . "\n";
   }
}
$obj = new MyDestructableClass();?>

 

What is static keyword?

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
Static properties cannot be accessed through the object using the arrow operator ->.
Calling non-static methods statically generates an E_STRICT level warning.
Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static). 

<?phpclass Foo{
    public static 
$my_static = 'foo';

    public function 
staticValue() {
        return 
self::$my_static;
    }
}

class 
Bar extends Foo{
    public function 
fooStatic() {
        return 
parent::$my_static;
    }
}


print 
Foo::$my_static . "\n";
$foo = new Foo();
print 
$foo->staticValue() . "\n";
print 
$foo->my_static . "\n";      // Undefined "Property" my_static
print $foo::$my_static . "\n";$classname = 'Foo';
print 
$classname::$my_static . "\n"; // As of PHP 5.3.0
print Bar::$my_static . "\n";$bar = new Bar();
print 
$bar->fooStatic() . "\n";?>

What is interface?

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
All methods declared in an interface must be public, this is the nature of an interface. 


<?php
// Declare the interface 'iTemplate'interface iTemplate{
    public function 
setVariable($name, $var);
    public function 
getHtml($template);
}
// Implement the interface
// This will work
class Template implements iTemplate{
    private 
$vars = array();
 
    public function 
setVariable($name, $var)
    {
        
$this->vars[$name] = $var;
    }
 
    public function 
getHtml($template)
    {
        foreach(
$this->vars as $name => $value) {
            
$template = str_replace('{' . $name . '}', $value, $template);
        }

        return 
$template;
    }
}
// This will not work
// Fatal error: Class BadTemplate contains 1 abstract methods
// and must therefore be declared abstract (iTemplate::getHtml)
class BadTemplate implements iTemplate{
    private 
$vars = array();
 
    public function 
setVariable($name, $var)
    {
        
$this->vars[$name] = $var;
    }
}
?>

What is abstract class?

PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation.
When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. 

<?phpabstract class AbstractClass{
    
// Force Extending class to define this method
    
abstract protected function getValue();
    abstract protected function 
prefixValue($prefix);

    
// Common method
    
public function printOut() {
        print 
$this->getValue() . "\n";
    }
}

class 
ConcreteClass1 extends AbstractClass{
    protected function 
getValue() {
        return 
"ConcreteClass1";
    }

    public function 
prefixValue($prefix) {
        return 
"{$prefix}ConcreteClass1";
    }
}

class 
ConcreteClass2 extends AbstractClass{
    public function 
getValue() {
        return 
"ConcreteClass2";
    }

    public function 
prefixValue($prefix) {
        return 
"{$prefix}ConcreteClass2";
    }
}
$class1 = new ConcreteClass1;$class1->printOut();
echo 
$class1->prefixValue('FOO_') ."\n";
$class2 = new ConcreteClass2;$class2->printOut();
echo 
$class2->prefixValue('FOO_') ."\n";?>

Wednesday, 14 April 2010

What is the difference between the functions unlink and unset?

unlink() is a function for file system handling. It will simply delete the file in context.

unset() is a function for variable management. It will make a variable undefined.


Cheers!

Tuesday, 13 April 2010

What is Final Keyword?

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.


<?php 
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }

   final public function 
moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class 
ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()

?>  


Similarly, You can stop overriding class using FINAL keyword. e.g., 


<?php
 final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }

   
// Here it doesn't matter if you specify the function as final or not
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class 
ChildClass extends BaseClass {
}
// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass) 

?>

 


Cheers!

Friday, 9 April 2010

How to Declare a Class and Initializing a Class Object ?

Declaring Class in PHP5:
In PHP5 you create a new class using "class" keyword.
The basic declaration of a class:


class myClass
{
   public function getAge()
   {
      echo "Inside Method";
   }
}
 
Initializing an Class Object in PHP5

To access the class from the outside world i.e. outside the scope of
class; you will need to create an object instance of the class. This
can be achieved using "new" operator.

Initializing Class 

class myClass
{
   public function myAge()
   {
      echo "Inside Method";
   }
}     
$myClassObj = new myClass();
 
//Calling class method... 
$myClassObj->myAge();