/    Sign up×
Community /Pin to ProfileBookmark

Simple OOP question

Ok guys,

I’ve written a few classes and need some help with them. Let’s say I have a ROW object. This object contains a method called getCell which returns a CELL object. My CELL class has a method called getData that returns a string. Thus:

$row->getCell(0) returns a CELL object
$cell->getData() returns a string

I want to do something like this:

echo( $row->getCell(0)->getData() );

However, PHP does not like this. What am I doing wrong?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@sydelctDec 27.2004 — I don't think there's a way to do that. Why not do it in two lines - makes it work and also makes it readable. ?

$cell = $row->getCell(0);

echo $cell->getData();

Mike
Copy linkTweet thisAlerts:
@telconstar99authorDec 28.2004 — I suppose I could use that workaround. PHPs OOP support in version 4 seems kinda weak. Ah well. Gotta work with what you have right?

I have another question. It's about overloaded functions. For instance, if I have a function called test and I want to overload it. I would want to do something like this:

function test($param1)

{

//Do stuff

}

function test($param1,$param2)

{

//Do even more stuff

}


That's not possible (if I understand PHP correctly). So how do I make it to where param2 is an optional parameter? I mean, look at PHPs explode function (http://us4.php.net/explode). It has an optional third parameter. How do I make it so that I can have optional parameters?
Copy linkTweet thisAlerts:
@AdamGundryDec 28.2004 — Yeah, PHP4 doesn't have the best OOP support in the world. (Version 5 is much better, I believe, and IIRC it supports overloading.) However, you can set default values for parameters like this:

function test($param1, $param2 = 'default')

Adam
Copy linkTweet thisAlerts:
@sydelctDec 28.2004 — php was not started as an OOP language but it is trying its best to support OOP. ? I myself seldom use OOP in PHP but there are cases that I create a set of classes to make things more reusable.

As for your question re functions, the previous example is more straightforward. However, if you want your function to accept an unknown number of parameters, then the function to use is func_get_args(). It returns all parameters passed to a function in an array.

This way, you can just do this:

function somefunction () {

$params = func_get_args ();

}

Another handy function is func_num_args() which returns the number of arguments passed to a function.

Mike
Copy linkTweet thisAlerts:
@telconstar99authorDec 28.2004 — So then if I have this:

function test($param1,$param2 = "default")

and somebody calls test("myValue") then the function test will be called properly even though the wrong number of params are passed to it?

Also, if somebody does this:

test("myValue", "myValue2") will $param2 now contain "myValue2" instead of "decault"?
Copy linkTweet thisAlerts:
@sydelctDec 28.2004 — you got it! yup that's right.
×

Success!

Help @telconstar99 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...