IT Community - Software Programming, Web Development and Technical Support

PHP Classes For Beginners

This is a discussion on PHP Classes For Beginners within the PHP Programming forums, part of the Web Development category; give simple example for interface in php?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #61 (permalink)  
Old 05-08-2008, 07:04 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

give simple example for interface in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #62 (permalink)  
Old 05-08-2008, 07:06 AM
sureshbabu sureshbabu is offline
D-Web Programmer
 
Join Date: Jul 2007
Location: India
Posts: 98
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Default Re: PHP Classes For Beginners

<?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;
}
}

?>
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #63 (permalink)  
Old 05-14-2008, 05:08 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

what is over loading in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #64 (permalink)  
Old 05-14-2008, 05:09 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Reusing the same method name in a class called overloading. The overloading method shares common method name and different return type and different parameter list.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #65 (permalink)  
Old 05-14-2008, 05:10 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

Explain Over loading in PHP 5?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #66 (permalink)  
Old 05-14-2008, 05:12 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Both method calls and member accesses can be overloaded via the __call, __get and __set methods. These methods will only be triggered when your object or inherited object doesn't contain the member or method you're trying to access. All overloading methods must not be defined as static. All overloading methods must be defined as public.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #67 (permalink)  
Old 05-14-2008, 05:14 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Member overloading

void __set ( string $name , mixed $value )
mixed __get ( string $name )
bool __isset ( string $name )
void __unset ( string $name )

Class members can be overloaded to run custom code defined in your class by defining these specially named methods. The $name parameter used is the name of the variable that should be set or retrieved. The __set() method's $value parameter specifies the value that the object should set the $name.
Note: The __set() method cannot take arguments by reference.
overloading with __get, __set, __isset and __unset example
PHP Code:
<?php
class Setter
{
    public 
$n;
    private 
$x = array("a" => 1"b" => 2"c" => 3);

    public function 
__get($nm)
    {
        echo 
"Getting [$nm]\n";

        if (isset(
$this->x[$nm])) {
            
$r $this->x[$nm];
            print 
"Returning: $r\n";
            return 
$r;
        } else {
            echo 
"Nothing!\n";
        }
    }

    public function 
__set($nm$val)
    {
        echo 
"Setting [$nm] to $val\n";

        if (isset(
$this->x[$nm])) {
            
$this->x[$nm] = $val;
            echo 
"OK!\n";
        } else {
            echo 
"Not OK!\n";
        }
    }

    public function 
__isset($nm)
    {
        echo 
"Checking if $nm is set\n";

        return isset(
$this->x[$nm]);
    }

    public function 
__unset($nm)
    {
        echo 
"Unsetting $nm\n";

        unset(
$this->x[$nm]);
    }
}

$foo = new Setter();
$foo->1;
$foo->100;
$foo->a++;
$foo->z++;

var_dump(isset($foo->a)); //true
unset($foo->a);
var_dump(isset($foo->a)); //false

// this doesn't pass through the __isset() method
// because 'n' is a public property
var_dump(isset($foo->n));

var_dump($foo);
?>
[output]
The above example will output:
Setting [a] to 100
OK!
Getting [a]
Returning: 100
Setting [a] to 101
OK!
Getting [z]
Nothing!
Setting [z] to 1
Not OK!

Checking if a is set
bool(true)
Unsetting a
Checking if a is set
bool(false)
bool(true)
[/output]
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #68 (permalink)  
Old 05-14-2008, 05:16 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Method overloading
mixed __call ( string $name , array $arguments )
The magic method __call() allows to capture invocation of non existing methods. That way __call() can be used to implement user defined method handling that depends on the name of the actual method being called. This is for instance useful for proxy implementations. The arguments that were passed in the function will be defined as an array in the $arguments parameter. The value returned from the __call() method will be returned to the caller of the method.
overloading with __call example
PHP Code:
<?php
class Caller
{
    private 
$x = array(123);

    public function 
__call($m$a)
    {
        print 
"Method $m called:\n";
        
var_dump($a);
        return 
$this->x;
    }
}

$foo = new Caller();
$a $foo->test(1"2"3.4true);
var_dump($a);
?>
The above example will output:

Method test called:
array(4) {
[0]=>
int(1)
[1]=>
string(1) "2"
[2]=>
float(3.4)
[3]=>
bool(true)
}
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #69 (permalink)  
Old 05-15-2008, 06:17 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

What is meaning for method overriding?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #70 (permalink)  
Old 05-15-2008, 06:21 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Overriding

Overriding a method means redefining a method inherited from the parent class in the child class.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #71 (permalink)  
Old 05-15-2008, 06:23 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how could we implement the overriding in the php programming?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #72 (permalink)  
Old 05-15-2008, 06:26 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

A simple example

PHP Code:
<?php
  
class Rectangle {
    public 
$height;
    public 
$width;
   
    public function 
__construct($width$height) {
      
$this->width $width;
      
$this->height $height;
     }
    
     public function 
getArea() {
      return 
$this->height $this->width;
     }
   }

  class 
Square extends Rectangle {
    public function 
__construct($size) {
      
$this->height $size;
      
$this->width $size;
    }
   
    public function 
getArea() {
      return 
pow($this->height2);
    }
   
  }

$obj = new Square(7);
$a $obj->getArea();
echo 
"$a";
?>
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #73 (permalink)  
Old 05-16-2008, 05:36 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

what is polymorphisms?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #74 (permalink)  
Old 05-16-2008, 05:38 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Polymorphisms
“The occurrence of different forms, stages, or types in individual organisms or in organisms of the same species, independent of sexual variations.” (dictionary.com). By that definition, we could assume polymorphism is a programmatic way to represent the same object through multiple states or stages. That’s great, but probably still unclear to many of us. What it really means is this: programming to an interface or base class without regard to an object’s concrete class.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #75 (permalink)  
Old 05-16-2008, 05:39 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

can u give me an real time examples for polymorphisms????
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #76 (permalink)  
Old 05-16-2008, 05:41 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

For example, consider a class called Person. We can subclass Person with classes called David, Charles, and Alejandro. Person has an abstract method called AcceptFeedback(), which all subclasses implement. That means that any code using any subclasses of the Person base class can call the AcceptFeedback() method with confidence, knowing that the class itself handles logic that would otherwise appear in a conditional. You do not have to check whether the object is a David or an Alejandro, just that it is a Person. The effect is that your code is written to the lowest common denominator – the Person class.

The Person class in this example could also be created as an interface. There are some differences, primarily that an interface imparts no behavior, only a set of rules, so to speak. A Person interface would say “you must support the AddFeedback() method” whereas a Person class could provide some default code for the AddFeedback() method, saying “if you choose not to support AddFeedback(), you will be provided with a default implementation.” Choosing interfaces or base classes is the subject for another article entirely, but in general, if you need a default implementation for a method, provide it through a base class. If you are simply outlining a set of expectations for your classes, then use an interface.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #77 (permalink)  
Old 05-16-2008, 05:43 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

How can we implement polymorphisms in PHP?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #78 (permalink)  
Old 07-13-2008, 11:53 PM
Miakoda Miakoda is offline
D-Web Trainee
 
Join Date: Jul 2008
Posts: 32
Miakoda is on a distinguished road
Default Re: PHP Classes For Beginners

i donot know How can we implement polymorphisms in PHP?
plz tell how to use constructor and destructor in programs?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #79 (permalink)  
Old 08-07-2008, 10:40 PM
suman suman is offline
D-Web Trainee
 
Join Date: Aug 2008
Posts: 27
suman is on a distinguished road
Default Re: PHP Classes For Beginners

thanks


i gain a lots of knowledge from all of you.
__________________
Free templates
web design company
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #80 (permalink)  
Old 08-10-2008, 10:58 PM
getzephyr getzephyr is offline
D-Web Programmer
 
Join Date: Dec 2007
Posts: 53
getzephyr is on a distinguished road
Default Re: PHP Classes For Beginners

Quote:
Originally Posted by saravanan View Post
how to use destructor in php program?
This class can be used to implement class destructor functionality in a way that it works under PHP 4.

It works as a base class that keeps track of all objects that are created by its subclasses.

If a PHP script attempts to exit before an object is destroyed, a shutdown function is called to explicitly destroy any pending objects.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes