Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Properties and Methods Constructor Method

Ben Anggoro
Ben Anggoro
36,161 Points

More OOP PHP Please...

Hello everyone, Hi Hampton,

I have problem understanding the idea of this and I don't get anything written on PHP manual as well. Dear Hampton, you are awesome but the example is just too simple and explaining nothing really especially for someone who just started learning this. i.e. I have totally no idea what you are on about :)

I keep having this 'I thought I get it but I was wrong' situation.

In my case now I'm trying to create custom excerpt for WP that can show excerpts with all its HTML tags

I've managed to get this working the way I want, but I feel like this maybe dumb way to do this. I think I did it the way it is now because I've been sass-ing a lot lately. So the way I did it was exactly like creating mixin with sass... :D

So I created 2 functions. One is to truncate and add necessary HTML tags to it then return the result. (I forked it somewhere but I can't remember where did I get that so I can't refer back to it.) This truncate function accept arguments.

Then just like using mixin, I created another function that calls this truncate function and gave it the actual arguments (some args getting its values from custom fields), then returning the desired 'custom excerpt'.

It works, but how I can apply OOP PHP in this case?

Big thanks and very much appreciate any guidance in advance

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Ben, I think for the use case you described, OOP might be a lot of overkill. For thing like simple text formatting and generating side navigation in WP I still use procedural programming. I just think that setting up a class, including it, and instantiating it just to format text is a little extreme. Now, if you were doing some extreme string formatting, like building your own shortcode generator, then I would say uses a class.

Ben Anggoro
Ben Anggoro
36,161 Points

Hi Andrew,

Thank you! I actually kind of thinking the same, but since I'm not a PHP expert I wasn't sure whether it's kind of too much for what it is or it was me not knowing how to do things. I think I'm getting there and just need to get used to use it so I would know when to use or not to use it.

The problem is there are tons of tutorials about this in the wild and these blogs are giving this idea that 'cool kids always wrap their functions in class' which I don't get myself sometimes why all these when you only want to do that but then again I'm not an expert. So... thanks again it's good to hear the thoughts of others.

And by the way again, please treehouse the title says itself, it's a deep dive. So I think we need to go deeper than 'Trout is excellent Fish' kind of example :D