Вы находитесь на странице: 1из 1

Envato Elements Envato Studio Community Forum Help Subscribe Sign In

How-To Tutorials  Courses  eBooks  Pricing Search Envato Tuts+ 

Monetize your site


With ads you can count on
Advertisement

CODE > PHP

Understanding and
Applying Polymorphism in
PHP
 FEED  LIKE  FOLLOW  FOLLOW
by Steve Guidetti 8 Sep 2010 Difficulty: Intermediate Length: Medium Languages: English

PHP Web Development OOP  Weekly email summary


Advertisement

Subscribe below and we’ll send you a


In object oriented programming, polymorphism is a powerful and fundamental weekly email summary of all new Code
tutorials. Never miss out on learning
tool. It can be used to create a more organic flow in your application. This about the next big thing.
tutorial will describe the general concept of polymorphism, and how it can
easily be deployed in PHP. WATCH ANY
Email Address
COURSE NOW
What is Polymorphism? Update me weekly

Subscribe to Access 
Polymorphism is a long word for a very simple concept.

Polymorphism describes a pattern in object


oriented programming in which classes have
different functionality while sharing a common
interface.

The beauty of polymorphism is that the code working with the different
classes does not need to know which class it is using since they're all used
the same way.

A real world analogy for polymorphism is a button. Everyone knows how to


use a button: you simply apply pressure to it. What a button "does," however,
depends on what it is connected to and the context in which it is used -- but
the result does not affect how it is used. If your boss tells you to press a
button, you already have all the information needed to perform the task.

In the programming world, polymorphism is used to make applications more


modular and extensible. Instead of messy conditional statements describing
different courses of action, you create interchangeable objects that you select
based on your needs. That is the basic goal of polymorphism.

Interfaces
Advertisement

An integral part of polymorphism is the common interface. There are two


ways to define an interface in PHP: interfaces and abstract classes. Both have Download Attachment
their uses, and you can mix and match them as you see fit in your class
hierarchy.

Translations
Interface
An interface is similar to a class except that it cannot contain code. An Envato Tuts+ tutorials are translated

into other languages by our community


interface can define method names and arguments, but not the contents of
members—you can be involved too!
the methods. Any classes implementing an interface must implement all
methods defined by the interface. A class can implement multiple interfaces. Translate this post

An interface is declared using the ' interface ' keyword: Powered by

1 interface MyInterface {
2 // methods
3 }

and is attached to a class using the ' implements ' keyword (multiple interfaces
can be implemented by listing them separated with commas):

1 class MyClass implements MyInterface {


2 // methods
3 }

Methods can be defined in the interface just like in a class, except without the
body (the part between the braces):

1 interface MyInterface {
2 public function doThis();
3 public function doThat();
4 public function setName($name);
5 }

All methods defined here will need to be included in any implementing


classes exactly as described. (read the code comments below)

01 // VALID
02 class MyClass implements MyInterface {
03 protected $name;
04 public function doThis() {
05 // code that does this
06 }
07 public function doThat() {
08 // code that does that
09 }
10 public function setName($name) {
11 $this->name = $name;
12 }
13 }
14
15 // INVALID
16 class MyClass implements MyInterface {
17 // missing doThis()!
18
19 private function doThat() {
20 // this should be public!
21 }
22 public function setName() {
23 // missing the name argument!
24 }
25 }

Abstract Class
An abstract class is a mix between an interface and a class. It can define
functionality as well as interface (in the form of abstract methods). Classes
extending an abstract class must implement all of the abstract methods
defined in the abstract class.

An abstract class is declared the same way as classes with the addition of
the ' abstract ' keyword:

1 abstract class MyAbstract {


2 // methods
3 }

and is attached to a class using the ' extends ' keyword:

1 class MyClass extends MyAbstract {


2 // class methods
3 }

Regular methods can be defined in an abstract class just like in a regular


class, as well as any abstract methods (using the ' abstract ' keyword).
Abstract methods behave just like methods defined in an interface, and must
be implemented exactly as defined by extending classes.

1 abstract class MyAbstract {


2 public $name;
3 public function doThis() {
4 // do this
5 }
6 abstract public function doThat();
7 abstract public function setName($name);
8 }

Step 1: Identify The Problem


Let's imagine that you have an Article class that is responsible for managing
articles on your website. It contains information about an article, including
the title, author, date, and category. Here's what it looks like:

01 class poly_base_Article {
02 public $title;
03 public $author;
04 public $date;
05 public $category;
06
07 public function __construct($title, $author, $date, $category = 0) {
08 $this->title = $title;
09 $this->author = $author;
10 $this->date = $date;
11 $this->category = $category;
12 }
13 }

Note: The example classes in this tutorial use the naming convention of
"package_component_Class." This is a common way to separate classes into
virtual namespaces to avoid name collisions.

Now you want to add a method to output the information into different
formats, such as XML and JSON. You might be tempted to do something like
this:

01 class poly_base_Article {
02 //...
03 public function write($type) {
04 $ret = '';
05 switch($type) {
06 case 'XML':
07 $ret = '<article>';
08 $ret .= '<title>' . $obj->title . '</title>';
09 $ret .= '<author>' . $obj->author . '</author>';
10 $ret .= '<date>' . $obj->date . '</date>';
11 $ret .= '<category>' . $obj->category . '</category>';
12 $ret .= '</article>';
13 break;
14 case 'JSON':
15 $array = array('article' => $obj);
16 $ret = json_encode($array);
17 break;
18 }
19 return $ret;
20 }
21 }

This is kind of an ugly solution, but it works -- for now. Ask yourself what
happens in the future, though, when we want to add more formats? You can
keep editing the class, adding more and more cases, but now you're only
diluting your class.

One important principle of OOP is that a class


should do one thing, and it should do it well.

With this in mind, conditional statements should be a red flag indicating that
your class is trying to do too many different things. This is where
polymorphism comes in.

In our example, it is clear that there are two tasks presented: managing
articles and formatting their data. In this tutorial, we will refactor our
formatting code into a new set of classes and discover how easy it is use
polymorphism.

Step 2: De ne Your Interface


The first thing we should do is define the interface. It is important to think
hard about your interface, because any changes to it may require changes to
calling code. In our example, we'll be using a simple interface to define our
one method:

1 interface poly_writer_Writer {
2 public function write(poly_base_Article $obj);
3 }

It's that simple; we have defined a public write() method that accepts an
Article object as an argument. Any classes implementing the Writer interface
will be sure to have this method.

Tip: If you want to restrict the type of arguments that can be passed to your
functions and methods, you can use type hints, as we've done in the write()

method; it only accepts objects of type poly_base_Article . Unfortunately,


return type hinting is not supported in current versions of PHP, so it is up to
you to take care of return values.

Step 3: Create Your Implementation


With your interface defined, it is time to create the classes that actually do
stuff. In our example, we have two formats that we want to output. Thus we
have two Writer classes: XMLWriter and JSONWriter. It's up to these to extract
the data from the passed Article object and format the information.

Here's what XMLWriter looks like:

01 class poly_writer_XMLWriter implements poly_writer_Writer {


02 public function write(poly_base_Article $obj) {
03 $ret = '<article>';
04 $ret .= '<title>' . $obj->title . '</title>';
05 $ret .= '<author>' . $obj->author . '</author>';
06 $ret .= '<date>' . $obj->date . '</date>';
07 $ret .= '<category>' . $obj->category . '</category>';
08 $ret .= '</article>';
09 return $ret;
10 }
11 }

As you can see from the class declaration, we use the implements keyword to
implement our interface. The write() method contains functionality specific
to formatting XML.

Now here's our JSONWriter class:

1 class poly_writer_JSONWriter implements poly_writer_Writer {


2 public function write(poly_base_Article $obj) {
3 $array = array('article' => $obj);
4 return json_encode($array);
5 }
6 }

All of our code specific to each format is now contained within individual
classes. These classes each have the sole responsibility of handling a
specific format, and nothing else. No other part of your application needs to
care about how these work in order to use it, thanks to our interface.

Step 4: Use Your Implementation


With our new classes defined, it's time to revisit our Article class. All the code
that lived in the original write() method has been factored out into our new
set of classes. All our method has to do now is to use the new classes, like
this:

1 class poly_base_Article {
2 //...
3 public function write(poly_writer_Writer $writer) {
4 return $writer->write($this);
5 }
6 }

All this method does now is accept an object of the Writer class (that is any
class implementing the Writer interface), call its write() method, passing
itself ( $this ) as the argument, then forward its return value straight to the
client code. It no longer needs to worry about the details of formatting data,
and it can focus on its main task.

Obtaining A Writer
You may be wondering where you get a Writer object to begin with, since you
need to pass one to this method. That's up to you, and there are many
strategies. For example, you might use a factory class to grab request data
and create an object:

01 class poly_base_Factory {
02 public static function getWriter() {
03 // grab request variable
04 $format = $_REQUEST['format'];
05 // construct our class name and check its existence
06 $class = 'poly_writer_' . $format . 'Writer';
07 if(class_exists($class)) {
08 // return a new Writer object
09 return new $class();
10 }
11 // otherwise we fail
12 throw new Exception('Unsupported format');
13 }
14 }

Like I said, there are many other strategies to use depending on your
requirements. In this example, a request variable chooses which format to
use. It constructs a class name from the request variable, checks if it exists,
then returns a new Writer object. If none exists under that name, an exception
is thrown to let client code figure out what to do.

Monetize your site


With ads you can count on

Advertisement

Step 5: Put It All Together


With everything in place, here is how our client code would put it all together:

01 $article = new poly_base_Article('Polymorphism', 'Steve', time(), 0);


02
03 try {
04 $writer = poly_base_Factory::getWriter();
05 }
06 catch (Exception $e) {
07 $writer = new poly_writer_XMLWriter();
08 }
09
10 echo $article->write($writer);

First we created an example Article object to work with. Then we try to get a
Writer object from the Factory, falling back to a default (XMLWriter) if an
exception is thrown. Finally, we pass the Writer object to our Article's write()

method, printing the result.

Conclusion
In this tutorial, I've provided you with an introduction to polymorphism and an
explanation of interfaces in PHP. I hope you realize that I've only shown you
one potential use case for polymorphism. There are many, many more
applications. Polymorphism is an elegant way to escape from ugly
conditional statements in your OOP code. It follows the principle of keeping
your components separate, and is an integral part of many design patterns. If
you have any questions, don't hesitate to ask in the comments!

WP Translation plugin

Compatible with all Theme & Plugins (incl. WooCommerce). SEO optimized. Join
20,000 users.

Advertisement

Steve Guidetti

Steve creates Web and Android apps. He is self-taught in PHP,


JavaScript, and a handful of other languages. You can see what he's
up to via his site at ultramegasoft.com.

107 Comments Nettuts+ 


5 Yusto

 Recommend 11 Important
⤤ Share Update Sort by Best

When you log in with Disqus, we process personal data to facilitate your ×
Nettuts+ requires you to verify your email address before posting. Send verification email to
authentication
omondiy@gmail.com
and posting of comments. We also store the comments you
post and those comments are immediately viewable and searchable by
anyone around the world.
I agree to Disqus' Terms of Service
Join the discussion…
I agree to Disqus' processing of email and IP address, and the use of
cookies, to facilitate my authentication and posting of comments,
explained further
Darius • 6 years ago in the Privacy Policy
Advertisement
I didn't understand well - what is the point of this. I still see that there is try catch block
- it tries to get one class, if it fails, Proceed
it gets another class.

And its written that this is good to avoid if else or switch statements.

But - it still same action with try catch. Just other keywords.

Instead of complicated multiple classes, its easier to understand if else or switch. And
we always want readability, not more mess in the code, don't we?

We could make it more complicated and show other how good we are, we understand
advanced concepts. But the more important thing is simplicity, so the code could be
written and understood faster.
19 △ ▽ • Reply • Share ›

eric_shinn > Darius • 5 years ago


That's a valid question, Darius. It's a little confusing in that there are only 2
possibilities for the outcome in this particular example. Instead, think of it this
way: Do we need the format in XML? JSON? CSV? DOCX? RTF?…(or one of
a couple hundred others)? With each new format you would have to add to the
IF/ELSE or SWITCH statement. Instead of riffling through a hundred
possibilities, it basically says, "I don't know what format you are, I don't want to
know - ask the 'Factory' instead. The Factory will take a guess based on a
URL parameter. But if it all screws up then come back and tell me and uh…
we'll just wing-it with XML m'kay?"

So if a request comes in with .com/article.php?format=JSON


…then it will comeback in JSON format.
comes ini with .com/article.php?format=XML
…then XML it is.
But if the request comes in with .com/article.php?
format=NannerSplitWitXtraSprinkelz
…um, yeah no. All out of sprinkles - have some XML instead.
10 △ ▽ • Reply • Share ›

peter > eric_shinn • a year ago


Mr. Shinn: I represent the interests of NSWXS and it is vital that you
comply with our with NSWXS policy in this matter as well as State and
Local regulatory law that applies. First, I am to thank you on behalf of
our Board of Trustees, for your courage and for your insights on the
night of November 3, 2013. You set something into motion that night,
Mr. Shinn with a characteristically thoughtful reply to what could have
been any layman's exchange on polymorphism interface
implementation with function types separated out by class And while I
don't know what that means, Mr. Shinn, on the record and for the
record, you inspired leader, Dr. Francis R. Dabies, to launch NYC's first
polmorphic marketing agency. With a 32% marketshare and 6030%
ROI return on market share, NannerSplitWitXtraSprinkelz Inc has
become the leading provider of polymorphic re-marketing services in
the five boroughs. As a courtesy, our legal team has recommended
that you be given the generous remainder of this week to remove
NannerSplitWitXtraSprinkelz from this page to prevent further fiduciary
infringement upon the rights of NSWXS. Thank you for your time, Mr.
Shinn.
△ ▽ • Reply • Share ›

Dustin • 8 years ago


Thanks Steve. You reminded me of an interview I went on five years ago at a growing
boutique agency in Soho, NYC. I was still getting my feet wet in object oriented
programming, yet decided to go for a Flash Developer position.

I was confident in every response, discussing my experience with client-side and


server-side technology, throwing around a few big words and making mention of the
Satay method and other ways to embed flash in html.

Then the lead developer paused and asked me to define polymorphism. I looked at
him as if he had three heads and got ready to get up from the chair I was squirming
in. Needless to say I didn't get the job and have done a lot more homework since. Still
have plenty to learn as far as web development goes. It's like Othello: a minute to
learn, a lifetime to master.
8△ ▽ • Reply • Share ›

peter > Dustin • a year ago


Thanks Dustin, you reminded me there of a job interview I had for a tech
company in Des Moines called Web Filings. People send them things, they file
them. Sounded simple enough, being on their web development team
sounded like a good fit. Besides, I knew SEO and could edit CSS you know,
like a full stack dev only with a WordPress background and no experience in
development whatsoever. I was disappointing not to have the skills I never
obtained and stepped down. The week I worked there I earned enough to take
my wife to the Rocky mountains for an additional week. But that's dumb luck.
More recently I've been obtain dev skills online, sites like thyis one, and they
are finally beginning to stick. But that's my story. Built some word press crap
and thought I was superman. Pathetic!
△ ▽ • Reply • Share ›

Veselin • 5 years ago


I've question....if we look at the last line ...$articule->write($writer)....this will do the
same right?...$writer->write($articule)...and in this case we write 1function less...I'm
rigth?
3△ ▽ • Reply • Share ›

deb0rian > Veselin • 5 years ago


Single responsibility principle. Basically what he did called Delegation.
△ ▽ • Reply • Share ›

John Milmine • 8 years ago


For all of you who liked this tutorial, rather than waiting for more, buy this book:
http://www.amazon.com/Desig..., read and understand and you'll be a lot better off in
your understanding of OO in general.

Matthew, agree that PHP is easy to use and there are a lot of frameworks that you
can use to speed things up. However understanding how a car works and how it is
put together is not the same as building the car from scratch. A good understanding
of how to design OO models will greatly improve the quality of your code. It also
means you're less likely to implement something that will be a pain to maintain down
the road.
2△ ▽ • Reply • Share ›

Philip Schroeder • 3 years ago


Where did this article talk about polymorphism?
There were interfaces and abstract classes, but polymorphism means, that functions
can be called with arbitrary datatypes, like in generic Java for instance.

I don't see the relation to abstract classes and interfaces, there is inheritance, but that
is not the same as polymorphism.
1△ ▽ • Reply • Share ›

Shehi > Philip Schroeder • 2 years ago


Polymorphism in general means using some ONE thing in MULTIPLE
DIFFERENT ways and shapes. You are talking about Parametric
Polymorphism, whereas the topic of this article is Ad-hoc Polymorphism.
△ ▽ • Reply • Share ›

Kemal DAĞ • 6 years ago


Until reading this article, I havent grasp the main idea behind interfaces, and I can
explain it in one sentence,

It allows you to create different classes sharing same blueprints, that you absolutely
now, the classes implementing same interface, will all have the abilities described by
interface.

One usage example will be, a general database interface, and different classes
implementing different protocols to interact with MySQL,PSQL, ORACLE and even a
custom database.

thanks for this extensive but yet simple explanation of interfaces.


1△ ▽ • Reply • Share ›

Raheel Khan > Kemal DAĞ • 4 years ago


The article goes above my head as i have an interview tomorrow and reading
it fast and fast didn't help me :D But your example is a strong example of
interface that i can give :D thanks
△ ▽ • Reply • Share ›

Amitay Horwitz • 8 years ago


Very nice. The only comment I have is this - as you said: "the class should do one
thing, and do it well". For that, I would not implement a "write" method for the Article
class (the article class should not care how it is used - it should only represent an
article).
And in the implementation code, I would simply use:

echo $writer->write($article);

Instead of:

echo $article->write($writer);

But great job, it's nice to see some serious programming articles around here from
time to time.
1△ ▽ • Reply • Share ›

Steve Guidetti > Amitay Horwitz • 8 years ago


Yeah, you're right. The only reason I did it this way was to show more clearly
where code was factored out.
△ ▽ • Reply • Share ›

Vino • 3 months ago


Could u provide a brief discussion of stock management by implementing
polymorphism and abstraction in php with an example program?
△ ▽ • Reply • Share ›

Ivan Zarechnii • 2 years ago


I do not agree with statement "One important principle of OOP is that a class should
do one thing, and it should do it well." its rather broad. And this example shows it
true however its a simple example. Life is more complicated and when learning OOP
from articles like this one can make conclusion that say you have class "dog" and
methods pee() bark() run() growl() according to this statement one should create
separate class for each "task", which is not any kind of OOP requirement and actually
anti-pattern.

I assume author of this article is referring to "Single responsibility principle" which


reads according to Wikipedia like this "... every class should have responsibility over a
single part of the functionality provided by the software, and that responsibility should
be entirely encapsulated by the class."

Does it sound same to you?


△ ▽ • Reply • Share ›

Guna Sekaran • 2 years ago


Really wonderful article. Though i have seen this example somewhere, this article
cleared my doubts on polymorphism. Thanks.
△ ▽ • Reply • Share ›

Jason • 3 years ago


I loved this article, but is was really hard for me to understand at first because on one
thing.

the poly_base_Article's function write had nothing to do with the Interface write
function except what it accepted. When first looking at it, it threw me for a loop.

I would recommend switching that name to something like "build" to avoid confusion
between the two functions. I also switched the $_request to a passed variable, since
we are already passing $title, $author, $date, $category. That way someone to test
the code and follow it through without adjusting the url for a get variable.

interface poly_writer_Writer {
public function build(poly_base_Article $obj);
}

class poly_base_Article {
public $title;
public $author;
public $date;
see more

△ ▽ • Reply • Share ›

NIck R • 3 years ago


Sorry but this tutorial defines polymorphism poorly.
You don't need interfaces to have polymorphism.
2 classes with the same function names but different functionality.
When one class(child) extends the other(parent) the method in the child will be called
first. You can also call the parent method from the child method. parent:method();
△ ▽ • Reply • Share ›

Shehi > NIck R • 2 years ago


It just doesn't really explain the concept of Polymorphism in a structured,
scientific way. Topic is rather Ad-hoc Polymorphism and that's all.
△ ▽ • Reply • Share ›

Mohamed Essam • 3 years ago


You helped me to understand new something
Thanks
△ ▽ • Reply • Share ›

mehmet • 4 years ago


here is terminal testable edition https://gist.github.com/mss...
△ ▽ • Reply • Share ›

Vegetta of the Philippines • 4 years ago


helpful thanks a lot.
△ ▽ • Reply • Share ›

Guest • 4 years ago


This isn't working, what's going on, I put the code into my editor the way it is here
△ ▽ • Reply • Share ›

jmvc • 4 years ago


I'm still confused. hahaha... one thing i learned though is that you created sets of
different classes with the same purpose(write) and separating there differences
logics(XML, JSON) instead of the usual if or else. I got confused on the last part.

echo $article->write($writer);

how the heck did $article have a "write" function. when $article is just a object which
contained variables( title, author, date)

shouldn't it be echo $writer->write($article) ???


△ ▽ • Reply • Share ›

jhjjgjgj • 4 years ago


bnnnvnnnn
△ ▽ • Reply • Share ›

gdggdg > jhjjgjgj • 4 years ago


fggdgdgdgdgdggd
△ ▽ • Reply • Share ›

vannaibell • 5 years ago


I don't understand....:)
△ ▽ • Reply • Share ›

bhavisya • 5 years ago


I didn't understand
△ ▽ • Reply • Share ›

Namal • 5 years ago


Thanks.....
△ ▽ • Reply • Share ›

Eric Lee • 6 years ago


Finally I know the usage of interface and implements in PHP.
Great example and thanks a lot!
△ ▽ • Reply • Share ›

Seph • 6 years ago


Great explanation!
△ ▽ • Reply • Share ›

Irfan Ansari • 6 years ago


Nice to see this tutorial on Nettuts. Put some more on it.
△ ▽ • Reply • Share ›

AA • 6 years ago
This is awesome stuff!
Thanks.
△ ▽ • Reply • Share ›

Mukesh • 6 years ago


Thanks a lot for writing such an easy to understand article !!
△ ▽ • Reply • Share ›

javed • 6 years ago


great job, it’s nice to see some serious programming articles ,,thanks
△ ▽ • Reply • Share ›

John • 6 years ago


Great explanation of polimorphism.

But, seriously, why the absurd complication in the example ?


Why not keep it simple and include the parameters in the interface signature or simply
creating an abstract class ?
This destroys the whole point of OOP by being unnecessarily cryptic.
△ ▽ • Reply • Share ›

Edgar Roberto • 7 years ago


Thank you for this article, great topic.......

Good introductory article about this specific design .

good work....
△ ▽ • Reply • Share ›

Peda • 7 years ago


Thanks for a good tutorial. I just want to ask, can we use this code:

$article = new poly_base_Article('Polymorphism', 'Steve', time(), 0);

try {
$writer = poly_base_Factory::getWriter();
}
catch (Exception $e) {
$writer = new poly_writer_XMLWriter();
}

echo $writer->write($article);

so why we need interface? Why upper code is no good?

Thanks
△ ▽ • Reply • Share ›

Prabeen Giri > Peda • 6 years ago


The reason why interface is used in above code is to maintain Liskov
Substitution principle which is OOP principle that is derived class can be
substituted by it base class and also SRP (single responsibility principle ).
Interface is used in above code to apply Strategy pattern. Interface in above
code create the hierarchy and encapsulates the strategies that is writeJson
and writeXML. The code where polymorphism is used ie. $writer->write() is not
aware of which write method is being called. Its only determined in the run
time and that makes dynamic binding.Interface is hiding these strategies and
creating and interface between these strategies and and the context where its
being called. Basic idea to use Interface is to apply polymorphism to create
open/close OOP principle.
2△ ▽ • Reply • Share ›

Marketing Online • 7 years ago


Excellent tutorials helped me a great help to solve my problem thanks and greetings
△ ▽ • Reply • Share ›

umer singhera • 7 years ago


nice stuff for beginers
△ ▽ • Reply • Share ›

Bojan • 7 years ago


Nice one, thanks a lot!
△ ▽ • Reply • Share ›

Luis Vera • 7 years ago


Thanks for this tut
△ ▽ • Reply • Share ›

Jeff • 7 years ago


Thanks - I just learned about the Strategy Pattern in a book and realized it was
exactly what I needed... but translating the book's example into a straightforward php
example was getting me all confused. Your example un-confused me and now I'm off
and running. thank you!!

And thanks to Tutorial City's comment that this is the Strategy Pattern. I was just
wondering if it was, and then I saw their comment!
△ ▽ • Reply • Share ›

Suraj Thkaur • 7 years ago


I am trying to understand interface the way you have tried to explain, I would say I
searched a lot and was not able to get so deep about interfaces.
Now, just have a question, what if in your above code you do not use interface at all.
Why you required interface for the above example, I am bit new to PHP OOPS.
Request you to please help me with it.

class poly_writer_XMLWriter {
public function write(poly_base_Article $obj) {
$ret = '';
$ret .= '' . $obj->title . '';
$ret .= '' . $obj->author . '';
$ret .= '' . $obj->date . '';
$ret .= '' . $obj->category . '';
$ret .= '';
return $ret;
}
}

l l it JSONW it {
see more

△ ▽ • Reply • Share ›

Fabian • 8 years ago


good job Steve!
△ ▽ • Reply • Share ›

evan-xg • 8 years ago


very good tuto, i use OO a lot in PHP but I still think that oop in php is not that OOP
as it stands I really suffer for type checking and casting . I foud a fallback using
reflexion but i think it php should include a restrictive mode for people willing to use
OOP in their application. without restriction on types you really just write OO code but
it just help mostly in code reusability. does' nt enforce the base rules of OOP
△ ▽ • Reply • Share ›

yasir • 8 years ago


thankru mmmmmmmmmuwa
△ ▽ • Reply • Share ›

Paginas web Guatemala • 8 years ago


Thanks for this tutorial! We need improve our web apps!! Have a nice Weekend! :)
△ ▽ • Reply • Share ›

Load more comments

✉ Subscribe d Add Disqus to your site 🔒 Disqus' Privacy Policy

LOOKING FOR SOMETHING TO HELP KICK START YOUR NEXT PROJECT?

Envato Market has a range of items for sale to help get you started.

WordPress Plugins PHP Scripts JavaScript


From $5 From $5 From $3

Unlimited Downloads Over 9 Million Digital Assets Hire a Freelancer


From $16.50/month Everything you need for your next Choose from 2,000 professionals
Get access to over 400,000 creative creative project. ready to do the work for you.
assets on Envato Elements.

QUICK LINKS - Explore popular categories

ENVATO TUTS+ JOIN OUR COMMUNITY HELP

About Envato Tuts+ Teach at Envato Tuts+ FAQ


Terms of Use Translate for Envato Tuts+ Help Center
26,240 1,147 26,097
Tutorials Courses Translations
Advertise Forums

Envato.com Our products Careers Sitemap


Follow Envato Tuts+
© 2018 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.

Вам также может понравиться