LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2008, 06:42 AM   #1
adilturbo
Member
 
Registered: Jun 2006
Location: morocco
Posts: 195

Rep: Reputation: 30
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.
 
Old 11-02-2008, 11:01 PM   #2
darkbolt
LQ Newbie
 
Registered: Sep 2005
Distribution: Gentoo, Debian, FreeBSD
Posts: 3

Rep: Reputation: 0
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.
 
Old 11-03-2008, 05:57 AM   #3
adilturbo
Member
 
Registered: Jun 2006
Location: morocco
Posts: 195

Original Poster
Rep: Reputation: 30
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.
 
Old 11-03-2008, 01:11 PM   #4
darkbolt
LQ Newbie
 
Registered: Sep 2005
Distribution: Gentoo, Debian, FreeBSD
Posts: 3

Rep: Reputation: 0
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(); 
 
Old 11-04-2008, 05:17 AM   #5
adilturbo
Member
 
Registered: Jun 2006
Location: morocco
Posts: 195

Original Poster
Rep: Reputation: 30
Ok thank you so much.

cheers.
 
  


Reply

Tags
php



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
installing src php5 or tar php5 step by step jatender Linux - Software 2 06-28-2007 10:29 AM
FC 6 - php5 idgeitman Fedora 1 01-08-2007 10:43 PM
Need Some Help With PHP5 mtndew Linux - Software 3 01-30-2006 10:07 PM
Php5 Thanotos Linux - Newbie 2 12-07-2005 10:39 AM
php5 Hampster Linux - Software 4 01-09-2005 04:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration