Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 1st, 2008, 03:55 AM
www.phpbasic.com
Guest
 
Posts: n/a
Default oop in php

i have 2 class:

class A{
function test($a,$b){ echo $a + $b; }
}
class B{
function test($a,$b){ echo $a * $b; }
}
how to define a class C and i can called as:
$c = new C;
$c->A->test(1,2) // call method test($a,$b) in class A
$c->B->test(2,3) // call method test($a,$b) in class B

And if class A,B has inputed in other file: a.php,b.php then i define
class C ?
  #2  
Old July 1st, 2008, 04:15 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: oop in php

www.phpbasic.com wrote:
Quote:
i have 2 class:
>
class A{
function test($a,$b){ echo $a + $b; }
}
class B{
function test($a,$b){ echo $a * $b; }
}
how to define a class C and i can called as:
$c = new C;
$c->A->test(1,2) // call method test($a,$b) in class A
$c->B->test(2,3) // call method test($a,$b) in class B
>
And if class A,B has inputed in other file: a.php,b.php then i define
class C ?
>
Well, you could:

class C {
public A = new A();
public B = new B();
}

But this violates several principals of OO and is generally not a good
design.

Stepping back - what is it you're really trying to do?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  #3  
Old July 1st, 2008, 06:25 AM
www.phpbasic.com
Guest
 
Posts: n/a
Default Re: oop in php

so, how to design class as
class A{
var $a;
var $b;
function A($a,$b){
$this->a = $a;
$this->b = $b;
}
funciton test(){
echo $a + $b;
}
}
$c->A(1,2)->test();
can do that?
On Jul 1, 10:10 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
www.phpbasic.comwrote:
Quote:
i have 2 class:
>
Quote:
class A{
function test($a,$b){ echo $a + $b; }
}
class B{
function test($a,$b){ echo $a * $b; }
}
how to define a class C and i can called as:
$c = new C;
$c->A->test(1,2) // call method test($a,$b) in class A
$c->B->test(2,3) // call method test($a,$b) in class B
>
Quote:
And if class A,B has inputed in other file: a.php,b.php then i define
class C ?
>
Well, you could:
>
class C {
public A = new A();
public B = new B();
>
}
>
But this violates several principals of OO and is generally not a good
design.
>
Stepping back - what is it you're really trying to do?
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==============
  #4  
Old July 1st, 2008, 11:45 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: oop in php

www.phpbasic.com wrote:
Quote:
On Jul 1, 10:10 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
>www.phpbasic.comwrote:
Quote:
>>i have 2 class:
>>class A{
>> function test($a,$b){ echo $a + $b; }
>>}
>>class B{
>> function test($a,$b){ echo $a * $b; }
>>}
>>how to define a class C and i can called as:
>>$c = new C;
>>$c->A->test(1,2) // call method test($a,$b) in class A
>>$c->B->test(2,3) // call method test($a,$b) in class B
>>And if class A,B has inputed in other file: a.php,b.php then i define
>>class C ?
>Well, you could:
>>
>class C {
> public A = new A();
> public B = new B();
>>
>}
>>
>But this violates several principals of OO and is generally not a good
>design.
>>
>Stepping back - what is it you're really trying to do?
>>
so, how to design class as
class A{
var $a;
var $b;
function A($a,$b){
$this->a = $a;
$this->b = $b;
}
funciton test(){
echo $a + $b;
}
}
$c->A(1,2)->test();
can do that?
(Top posting fixed)

No, because the A method doesn't return anything. Also, depending on
your version of PHP, method A could be a constructor or not - and you
shouldn't be calling a constructor yourself.

The way to do this would be just

class A{
functon test(){
return $a + $b;
}
}

$c = new A().
echo A->test(1, 2);

No need to save values - in fact, there's really no need for a class in
this case.

But again, you haven't explained exactly what you're really trying to
do. That would help steer you the right way.

Also, please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  #5  
Old July 1st, 2008, 11:45 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: oop in php

www.phpbasic.com wrote:
Quote:
On Jul 1, 10:10 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
>www.phpbasic.comwrote:
Quote:
>>i have 2 class:
>>class A{
>> function test($a,$b){ echo $a + $b; }
>>}
>>class B{
>> function test($a,$b){ echo $a * $b; }
>>}
>>how to define a class C and i can called as:
>>$c = new C;
>>$c->A->test(1,2) // call method test($a,$b) in class A
>>$c->B->test(2,3) // call method test($a,$b) in class B
>>And if class A,B has inputed in other file: a.php,b.php then i define
>>class C ?
>Well, you could:
>>
>class C {
> public A = new A();
> public B = new B();
>>
>}
>>
>But this violates several principals of OO and is generally not a good
>design.
>>
>Stepping back - what is it you're really trying to do?
>>
so, how to design class as
class A{
var $a;
var $b;
function A($a,$b){
$this->a = $a;
$this->b = $b;
}
funciton test(){
echo $a + $b;
}
}
$c->A(1,2)->test();
can do that?
(Top posting fixed)

No, because the A method doesn't return anything. Also, depending on
your version of PHP, method A could be a constructor or not - and you
shouldn't be calling a constructor yourself.

The way to do this would be just

class A{
function test(){
return $a + $b;
}
}

$c = new A().
echo A->test(1, 2);

No need to save values - in fact, there's really no need for a class in
this case.

But again, you haven't explained exactly what you're really trying to
do. That would help steer you the right way.

Also, please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  #6  
Old July 1st, 2008, 11:55 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: oop in php

Sorry for the duplicate post. My new server didn't reply to the send
first send request.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  #7  
Old July 1st, 2008, 12:05 PM
Floortje
Guest
 
Posts: n/a
Default Re: oop in php

Jerry Stuckle wrote:
Quote:
The way to do this would be just
>
class A{
functon test(){
return $a + $b;
}
}
>
$c = new A().
echo A->test(1, 2);
Not trying to be smart but I thought it was

echo $c->test(1,2);

Or am I missing some php oop language construct ?

floortje
  #8  
Old July 1st, 2008, 12:15 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: oop in php

Floortje wrote:
Quote:
Jerry Stuckle wrote:
>
Quote:
>The way to do this would be just
>>
>class A{
> functon test(){
> return $a + $b;
> }
> }
>>
>$c = new A().
>echo A->test(1, 2);
>
Not trying to be smart but I thought it was
>
echo $c->test(1,2);
>
Or am I missing some php oop language construct ?
>
floortje
>
Yep, you're right. I shouldn't try to do this before my first cup of
joe. :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles