Connecting Tech Pros Worldwide Help | Site Map

oop in php

www.phpbasic.com
Guest
 
Posts: n/a
#1: Jul 1 '08
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 ?
Jerry Stuckle
Guest
 
Posts: n/a
#2: Jul 1 '08

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
==================

www.phpbasic.com
Guest
 
Posts: n/a
#3: Jul 1 '08

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
==============
Jerry Stuckle
Guest
 
Posts: n/a
#4: Jul 1 '08

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
==================

Jerry Stuckle
Guest
 
Posts: n/a
#5: Jul 1 '08

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
==================

Jerry Stuckle
Guest
 
Posts: n/a
#6: Jul 1 '08

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
==================

Floortje
Guest
 
Posts: n/a
#7: Jul 1 '08

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
Jerry Stuckle
Guest
 
Posts: n/a
#8: Jul 1 '08

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
==================

Closed Thread