php classes help

droctoganapus86

New Member
Messages
49
Reaction score
0
Points
0
I have made a script, but it has become so big, that i want to convert it to a class. But I really don't understand how that works with variables and functions.

Basically: How do i make a class out of this?

I have this code :
PHP:
<div id="fb-root">
	<?php
	echo $fb->status;
	if ($fbuser):
		foreach ($fbhome['data'] as $mkey => $mvalue):
				$href = $fbhome['data'][$mkey]['from']['id'];
				$name = $fbhome['data'][$mkey]['from']['name'];
				$message = $fbhome['data'][$mkey]['message'];
				$comments = $fbhome['data'][$mkey]['comments'];
				$appname = $fbhome['data'][$mkey]['application']['name'];
				$action = $fbhome['data'][$mkey]['actions'][0]['link'];
				$likes = $fbhome['data'][$mkey]['likes']
				#$date = new datemaker($fbhome['data'][$mkey]['created_time']);
				?>
				<div class="post">
					<a href="http://www.facebook.com/profile.php?id=<?php echo $href; ?>"><?php echo $name; ?></a>
					<span class="message">
						<?php echo $message;?>
					</span>
					<span class="links">
						<?php if (isset($appname)):
							echo $appname;
						?>
						·
						<?php endif; ?>
						<a href="<?php echo $action; ?>">Like</a>&nbsp;·
						<a href="<?php echo $action; ?>">Comment</a>
					</span>
					<ul class="comment">
						<?php if (isset($comments['data']) || isset($likes)): ?>
							<li class="arrow">&nbsp;</li>
						<?php endif; ?>
						
						<?php if (isset ($likes)): ?>
							<li class="links">
								<?php if (count($likes['data']) == 1): ?>
									<a href="http://www.facebook.com/profile.php?id=<?php echo $likes['data'][0]['id']; ?>"><?php echo $likes['data'][0]['name']; ?></a>
									<?if ($likes['count'] > 1): ?>
										and <?php echo $likes['count']-1; ?> other like this.
									<?php else: ?>
										likes this.
									<?php endif; ?>
								<?php elseif(count($likes['data']) == 2) :
									foreach ($likes['data'] as $lkey => $lvalue): ?>
										<a href="http://www.facebook.com/profile.php?id=<?php echo $likes['data'][$lkey]['id']; ?>"><?php echo $likes['data'][$lkey]['name']; ?></a>,
									<?php endforeach;
									if ($likes['count'] > 2): ?>
										and <?php echo $likes['count']-2;?> other
									<?php endif; ?>
									like this.
								<?php endif;?>
							</li>
						<?php endif; ?>
						
						<?php if (isset($comments['data'])):
							if ($comments['count'] > 2):?>
								<li class="links"><a href="<?php echo $action ?>">View all <?php echo $comments['count']; ?> comments</a></li>
							<?php endif;
							foreach ($comments['data'] as $ckey => $cvalue):?>
								<li><a href="http://www.facebook.com/profile.php?id=<?php echo $comments['data'][$ckey]['from']['id']; ?>"><?php echo $comments['data'][$ckey]['from']['name']; ?></a>
								<span><?php echo $comments['data'][$ckey]['message']; ?></span>
							<?php endforeach;
						endif; ?>
					</ul>
				</div>
			<?php endforeach;
		endif; ?>
</div>
also, if anyone has suggestions to make this better than you're welcome :smile:
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The concepts in OOP differ fundamentally from procedural programming. While any procedural program could be transformed into an OO one mechanically, the result will be low quality, according to OO design principles and best practices. Your best bet is to first study OO principles, then create a new design, then refactor existing code to match the new design. Read up on OOP features (abstraction, encapsulation, inheritance, polymorphism) and the SOLID principles.

One issue with converting the sample is that it's not procedural. It defines no functions and uses only one (count; isset isn't a function but rather a language construct, also known as a special form). The code sample has no obvious architectural design. It wouldn't be so much a matter of conversion as construction.

As for how variables and functions work in pure OOP, you don't have free functions and variables. Instead, every variable and function is bound to an object. Classes basically add new scopes: in addition to global and function scope, you have class and object scope. PHP isn't pure OOP, so you'll still have free functions and variables, but these should mostly be the ones PHP itself creates.
 
Top