LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   upcasting in php5 (https://www.linuxquestions.org/questions/programming-9/upcasting-in-php5-679988/)

adilturbo 10-30-2008 06:42 AM

upcasting in php5
 
Hello,

i want to translate this code of java to PHP5:

Code:

interface I{
  //...
  abstract void methode1();
}

abstract class A implements I{
  void methode1(){
      methode2();
  }

  abstract void methode2();
}

class C extends A{
  void methode2(){
  //do some...
  }

}

i want to use this this way:

Code:

I i=new A(); //here is implicit upcasting
i.methode1(); //  this methode will implecitely call methode2 of class A

how can i do upcasting in PHP5?

thanks in advance.

darkbolt 11-02-2008 11:01 PM

If I'm understanding you right, what you want is something like

PHP Code:

<?php
class A{
   function 
foo(){
       echo(
'foo');
   }
}

class 
extends A{
    function 
foobar(){
        
parent::foo();
        echo(
'bar');
    }
}
$i = new B();
$i.foobar();
?>

Note, not tested, just written off the top of my head as I was reading this. ymmv.

adilturbo 11-03-2008 05:57 AM

Hello,

thanks for the replay.

i want to do upcasting. in java or c++ if class A inherits from class B this statement is true:

B b= new A();

i do not know how to do it in PHP.

darkbolt 11-03-2008 01:11 PM

PHP is dynamically typed, so you don't have to declare that it is a B even though you're creating an A, just simply create the A

PHP Code:

$a = new A(); 


adilturbo 11-04-2008 05:17 AM

Ok thank you so much.

cheers.


All times are GMT -5. The time now is 06:56 PM.