Connecting Tech Pros Worldwide Help | Site Map

oop in php

  #1  
Old July 1st, 2008, 03:55 AM
www.phpbasic.com
Guest
 
Posts: n/a
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

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

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

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

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

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

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to OOP in PHP. Where to start? aym answers 3 October 24th, 2007 02:44 AM
book/link recommendation for learning OO or MVC in PHP Notgiven answers 2 April 4th, 2006 09:55 AM
Classes in PHP Ameen_France answers 5 February 7th, 2006 08:15 PM
OO in PHP Mudge answers 51 July 17th, 2005 10:38 AM