/    Sign up×
Community /Pin to ProfileBookmark

Please help with Objects

I have two seperate main classes for different parts of a website.

Both classes are used to make a single object each.

Each of those objects utilizes a shared “sub”class to make lots of small objects that are used for easy storage and retrieval of data.

To keep them clean and small these “sub”objects call back to the parent in when more complex methods need to be run.

Problem is thus.

How do I:
refer back to the objects creator in order to access its methods?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@Alan01252May 08.2012 — Assuming you have this
[code=php]
class MyParent
{
public function doSomething()
{
//do something
}
}

class MyChild extends myParent
{
public function doSomething()
{
//do something slightly different in the child
parent::doSomething(); //then do exactly what the parent did.
}
}

$myChild = new MyChild();
$myChild->doSomething();
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 08.2012 — If it's not a parent:: issue as Alan suggested, then we may need a few more details about what actually needs to happen. Perhaps you'll need to pass $this as a parameter to the "small object", which it would then pass to its parent?
[code=php]
<?php

class TheParent
{
public function test(iFoo $foo)
{
return $foo->getText();
}
}

class TheChild extends TheParent
{
public $name = 'World';
}

interface iFoo
{
public function getText();
}

class Foo implements iFoo
{
private $obj;
public function getText()
{
return "Hello, " . $this->obj->name;
}
public function createObject()
{
$this->obj = new TheChild();
}
public function useObject()
{
echo $this->obj->test($this);
}
}

$foo = new Foo();
$foo->createObject();
$foo->useObject();
[/code]

Though this sort of circularity would make me think twice about this part of the application architecture. ?
Copy linkTweet thisAlerts:
@BelrickauthorMay 08.2012 — Yeah all to true nodog.

If i have a really large class

And a small class that extends the large class.

What will that mean for system resources should i make hundreds of objects from the small class?
Copy linkTweet thisAlerts:
@NogDogMay 09.2012 — Hundreds of small objects from the same class probably does not really do anything significantly more processing-wise than creating the same number of arrays. If each object were derived from a different class and thus required 100 different class definition files to be require()'d, I might be a little worried, but even then that sort of performance issue is generally lost in the noise compared to data issues. While it never hurts to at least briefly consider performance issues at this level, the danger is spending a lot of time fiddling with it only to find the PHP parser/compiler does a better job of optimizing things than you do. ?
Copy linkTweet thisAlerts:
@BelrickauthorMay 09.2012 — Cool.

In that case

It seems like i was over compensating!


But a more exact question.

Is an object created from a class that extends from a parent going to use more memory and or system processor than that of a same class that doesnt extend?
Copy linkTweet thisAlerts:
@NogDogMay 09.2012 — There is probably a [I]little[/I] overhead, but it's nothing I've ever worried about; but if you ever get the sort of traffic load where it would actually matter, you can probably afford a 2nd server along with a load-balancer. (Hardware is cheap these days compared to paying programmers for weeks/months of debugging code that is poorly designed/written. ? )

For that matter, PHP arrays are somewhat notorious for using excessive memory (at least in some people's view), so you may still be ahead of the curve by creating objects custom-designed for the specific data you want to store/manipulate -- again pointing to the fact that fiddling around with micro-optimizations can be tricky and sometimes counter-intuitive. (If you really need to worry about every micro-second for a missile guidance system or such, you probably shouldn't be using PHP anyway. ? )

Ultimately, I'm generally in favor of designing/coding for maintainability, and worrying about optimization only if/when you find you need it (though you always have it sort of kicking around in the back of your brain to make sure you aren't doing anything really stupid).
Copy linkTweet thisAlerts:
@BelrickauthorMay 09.2012 — THankyou nodog.

Appreciate the help.

WHile i use OOP a LOT i keep to my comfort zones and ignore much of its features.
Copy linkTweet thisAlerts:
@Alan01252May 09.2012 — Why? Reach out and you'll find your development practices and your code gets much better by using all the features of object orientated code.
×

Success!

Help @Belrick 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...