PHP 8 Objects, Patterns, and Practice: Mastering OO Enhancements, Design Patterns, and Essential Development Tools Zandstra download
PHP 8 Objects, Patterns, and Practice: Mastering OO Enhancements, Design Patterns, and Essential Development Tools Zandstra download
https://textbookfull.com/product/php-8-objects-patterns-and-
practice-mastering-oo-enhancements-design-patterns-and-essential-
development-tools-zandstra/
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-
loucas/
https://textbookfull.com/product/mastering-php-design-patterns-
develop-robust-and-reusable-code-using-a-multitude-of-design-
patterns-for-php-7-ali/
https://textbookfull.com/product/design-patterns-in-php-and-
laravel-1st-edition-kelt-dockins/
https://textbookfull.com/product/design-patterns-by-tutorials-
learning-design-patterns-in-swift-4-2-joshua-greene/
https://textbookfull.com/product/javascript-patterns-build-
better-applications-with-coding-and-design-patterns-1st-edition-
stefanov-stoyan/
https://textbookfull.com/product/design-patterns-by-tutorials-
third-edition-learning-design-patterns-in-swift-raywenderlich-
tutorial-team/
https://textbookfull.com/product/architectural-patterns-uncover-
essential-patterns-in-the-most-indispensable-realm-of-enterprise-
architecture-1st-edition-pethuru-raj/
https://textbookfull.com/product/java-design-patterns-a-tour-
of-23-gang-of-four-design-patterns-in-java-1st-edition-vaskaran-
sarcar/
Matt Zandstra
The publisher, the authors and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
In July 2004, PHP 5.0 was released. This version introduced a suite of
radical enhancements. Perhaps first among these was radically
improved support for object-oriented programming. This stimulated
much interest in objects and design within the PHP community. In fact,
this was an intensification of a process that began when version 4 first
made object-oriented programming with PHP a serious reality.
In this chapter, I look at some of the needs that coding with objects
can address. I very briefly summarize some aspects of the evolution of
patterns and related practices.
I also outline the topics covered by this book. I will look at the
following:
The evolution of disaster: A project goes bad
Design and PHP: How object-oriented design techniques took root
in the PHP community
This book: Objects, Patterns, Practice
The Problem
The problem is that PHP is just too easy. It tempts you to try out your
ideas and flatters you with good results. You write much of your code
straight into your web pages, because PHP is designed to support that.
You add utility functions (such as database access code) to files that can
be included from page to page, and before you know it, you have a
working web application.
You are well on the road to ruin. You don’t realize this, of course,
because your site looks fantastic. It performs well, your clients are
happy, and your users are spending money.
Trouble strikes when you go back to the code to begin a new phase.
Now you have a larger team, some more users, and a bigger budget. Yet,
without warning, things begin to go wrong. It’s as if your project has
been poisoned.
Your new programmer is struggling to understand code that is
second nature to you, although perhaps a little byzantine in its twists
and turns. She is taking longer than you expected to reach full strength
as a team member.
A simple change, estimated at a day, takes three days when you
discover that you must update 20 or more web pages as a result.
One of your coders saves his version of a file over major changes
you made to the same code some time earlier. The loss is not discovered
for three days, by which time you have amended your own local copy. It
takes a day to sort out the mess, holding up a third developer who was
also working on the file.
Because of the application’s popularity, you need to shift the code to
a new server. The project has to be installed by hand, and you discover
that file paths, database names, and passwords are hard-coded into
many source files. You halt work during the move because you don’t
want to overwrite the configuration changes the migration requires.
The estimated two hours becomes eight as it is revealed that someone
did something clever involving the Apache module ModRewrite, and
the application now requires this to operate properly.
You finally launch phase 2. All is well for a day and a half. The first
bug report comes in as you are about to leave the office. The client
phones minutes later to complain. Her report is similar to the first, but
a little more scrutiny reveals that it is a different bug causing similar
behavior. You remember the simple change back at the start of the
phase that necessitated extensive modifications throughout the rest of
the project.
You realize that not all of the required modifications are in place.
This is either because they were omitted to start with or because the
files in question were overwritten in merge collisions. You hurriedly
make the modifications needed to fix the bugs. You’re in too much of a
hurry to test the changes, but they are a simple matter of copy and
paste, so what can go wrong?
The next morning, you arrive at the office to find that a shopping
basket module has been down all night. The last-minute changes you
made omitted a leading quotation mark, rendering the code unusable.
Of course, while you were asleep, potential customers in other time
zones were wide awake and ready to spend money at your store. You fix
the problem, mollify the client, and gather the team for another day’s
firefighting.
This everyday tale of coding folk may seem a little over the top, but I
have seen all these things happen over and over again. Many PHP
projects start their life small and evolve into monsters.
Because the presentation layer also contains application logic,
duplication creeps in early as database queries, authentication checks,
form processing, and more are copied from page to page. Every time a
change is required to one of these blocks of code, it must be made
everywhere that the code is found, or bugs will surely follow.
Lack of documentation makes the code hard to read, and lack of
testing allows obscure bugs to go undiscovered until deployment. The
changing nature of a client’s business often means that code evolves
away from its original purpose until it is performing tasks for which it
is fundamentally unsuited. Because such code has often evolved as a
seething, intermingled lump, it is hard, if not impossible, to switch out
and rewrite parts of it to suit the new purpose.
Now, none of this is bad news if you are a freelance PHP consultant.
Assessing and fixing a system like this can fund expensive espresso
drinks and DVD box sets for six months or more. More seriously,
though, problems of this sort can mean the difference between a
business’s success and failure.
Father Antonio had been down through the streets of the old
town of Sorrento, searching for the young stonecutter, and finding
him had spent some time in enlightening him as to the details of the
work he wished him to execute.
He found him not so easily kindled into devotional fervors as he
had fondly imagined, nor could all his most devout exhortations
produce one quarter of the effect upon him that resulted from the
discovery that it was the fair Agnes who originated the design and
was interested in its execution. Then did the large black eyes of the
youth kindle into something of sympathetic fervor, and he willingly
promised to do his very best at the carving.
"I used to know the fair Agnes well, years ago," he said, "but of
late she will not even look at me; yet I worship her none the less.
Who can help it that sees her? I don't think she is so hard-hearted
as she seems; but her grandmother and the priests won't so much
as allow her to lift up her eyes when one of us young fellows goes
by. Twice these five years past have I seen her eyes, and then it was
when I contrived to get near the holy water when there was a press
round it of a saint's day, and I reached some to her on my finger,
and then she smiled upon me and thanked me. Those two smiles
are all I have had to live on for all this time. Perhaps, if I work very
well, she will give me another, and perhaps she will say, 'Thank you,
my good Pietro!' as she used to, when I brought her birds' eggs or
helped her across the ravine, years ago."
"Well, my brave boy, do your best," said the monk, "and let the
shrine be of the fairest white marble. I will be answerable for the
expense; I will beg it of those who have substance."
"So please you, holy father," said Pietro, "I know of a spot, a little
below here on the coast, where was a heathen temple in the old
days; and one can dig therefrom long pieces of fair white marble, all
covered with heathen images. I know not whether your Reverence
would think them fit for Christian purposes."
"So much the better, boy! so much the better!" said the monk,
heartily. "Only let the marble be fine and white, and it is as good as
converting a heathen any time to baptize it to Christian uses. A few
strokes of the chisel will soon demolish their naked nymphs and
other such rubbish, and we can carve holy virgins, robed from head
to foot in all modesty, as becometh saints."
"I will get my boat and go down this very afternoon," said Pietro;
"and, sir, I hope I am not making too bold in asking you, when you
see the fair Agnes, to present unto her this lily, in memorial of her
old playfellow."
"That I will, my boy! And now I think of it, she spoke kindly of
you as one that had been a companion in her childhood, but said
her grandmother would not allow her to speak to you now."
"Ah, that is it!" said Pietro. "Old Elsie is a fierce old kite, with
strong beak and long claws, and will not let the poor girl have any
good of her youth. Some say she means to marry her to some rich
old man, and some say she will shut her up in a convent, which I
should say was a sore hurt and loss to the world. There are a plenty
of women, whom nobody wants to look at, for that sort of work; and
a beautiful face is a kind of psalm which makes one want to be
good."
"Well, well, my boy, work well and faithfully for the saints on this
shrine, and I dare promise you many a smile from this fair maiden;
for her heart is set upon the glory of God and his saints, and she will
smile on any one who helps on the good work. I shall look in on you
daily for a time, till I see the work well started."
So saying, the old monk took his leave. Just as he was passing
out of the house, some one brushed rapidly by him, going down the
street. As he passed, the quick eye of the monk recognized the
cavalier whom he had seen in the garden but a few evenings before.
It was not a face and form easily forgotten, and the monk followed
him at a little distance behind, resolving, if he saw him turn in
anywhere, to follow and crave an audience of him.
Accordingly, as he saw the cavalier entering under the low arch
that led to his hotel, he stepped up and addressed him with a
gesture of benediction.
"God bless you, my son!"
"What would you with me, father?" said the cavalier, with a hasty
and somewhat suspicious glance.
"I would that you would give me an audience of a few moments
on some matters of importance," said the monk, mildly.
The tones of his voice seemed to have excited some vague
remembrance in the mind of the cavalier; for he eyed him narrowly,
and seemed trying to recollect where he had seen him before.
Suddenly a light appeared to flash upon his mind; for his whole
manner became at once more cordial.
"My good father," he said, "my poor lodging and leisure are at
your service for any communication you may see fit to make."
So saying, he led the way up the damp, ill-smelling stone
staircase, and opened the door of the deserted room where we have
seen him once before. Closing the door, and seating himself at the
one rickety table which the room afforded, he motioned to the monk
to be seated also; then taking off his plumed hat, he threw it
negligently on the table beside him, and passing his white, finely
formed hand through the black curls of his hair, he tossed them
carelessly from his forehead, and, leaning his chin in the hollow of
his hand, fixed his glittering eyes on the monk in a manner that
seemed to demand his errand.
"My Lord," said the monk, in those gentle, conciliating tones
which were natural to him, "I would ask a little help of you in regard
of a Christian undertaking which I have here in hand. The dear Lord
hath put it into the heart of a pious young maid of this vicinity to
erect a shrine to the honor of our Lady and her dear Son in this
gorge of Sorrento, hard by. It is a gloomy place in the night, and
hath been said to be haunted by evil spirits; and my fair niece, who
is full of all holy thoughts, desired me to draw the plan for this
shrine, and, so far as my poor skill may go, I have done so. See,
here, my Lord, are the drawings."
The monk laid them down on the table, his pale cheek flushing
with a faint glow of artistic enthusiasm and pride, as he explained to
the young man the plan and drawings.
The cavalier listened courteously, but without much apparent
interest, till the monk drew from his portfolio a paper and said,—
"This, my Lord, is my poor and feeble conception of the most
sacred form of our Lady, which I am to paint for the centre of the
shrine."
He laid down the paper, and the cavalier, with a sudden
exclamation, snatched it up, looking at it eagerly.
"It is she!" he said; "it is her very self!—the divine Agnes,—the
lily flower,—the sweet star,—the only one among women!"
"I see you have recognized the likeness," said the monk,
blushing. "I know it hath been thought a practice of doubtful
edification to represent holy things under the image of aught
earthly; but when any mortal seems especially gifted with a
heavenly spirit outshining in the face, it may be that our Lady
chooses that person to reveal herself in."
The cavalier was gazing so intently on the picture that he
scarcely heard the apology of the monk; he held it up, and seemed
to study it with a long admiring gaze.
"You have great skill with your pencil, my father," he said; "one
would not look for such things from under a monk's hood."
"I belong to the San Marco in Florence, of which you may have
heard," said Father Antonio, "and am an unworthy disciple of the
traditions of the blessed Angelico, whose visions of heavenly things
are ever before us; and no less am I a disciple of the renowned
Savonarola, of whose fame all Italy hath heard before now."
"Savonarola?" said the other, with eagerness,—"he that makes
these vile miscreants that call themselves Pope and cardinals
tremble? All Italy, all Christendom, is groaning and stretching out the
hand to him to free them from these abominations. My father, tell
me of Savonarola: how goes he, and what success hath he?"
"My son, it is now many months since I left Florence; since which
time I have been sojourning in by-places, repairing shrines and
teaching the poor of the Lord's flock, who are scattered and
neglected by the idle shepherds, who think only to eat the flesh and
warm themselves with the fleece of the sheep for whom the Good
Shepherd gave his life. My duties have been humble and quiet; for it
is not given to me to wield the sword of rebuke and controversy, like
my great master."
"And you have not heard, then," said the cavalier, eagerly, "that
they have excommunicated him?"
"I knew that was threatened," said the monk, "but I did not think
it possible that it could befall a man of such shining holiness of life,
so signally and openly owned of God that the very gifts of the first
Apostles seem revived in him."
"Does not Satan always hate the Lord?" said the cavalier.
"Alexander and his councils are possessed of the Devil, if ever men
were,—and are sealed as his children by every abominable
wickedness. The Devil sits in Christ's seat, and hath stolen his
signet-ring, to seal decrees against the Lord's own followers. What
are Christian men to do in such case?"
The monk sighed and looked troubled.
"It is hard to say," he answered. "So much I know,—that before I
left Florence our master wrote to the King of France touching the
dreadful state of things at Rome, and tried to stir him up to call a
general council of the Church. I much fear me this letter may have
fallen into the hands of the Pope."
"I tell you, father," said the young man, starting up and laying his
hand on his sword, "we must fight! It is the sword that must decide
this matter! Was not the Holy Sepulchre saved from the Infidels by
the sword?—and once more the sword must save the Holy City from
worse infidels than the Turks. If such doings as these are allowed in
the Holy City, another generation there will be no Christians left on
earth. Alexander and Cæsar Borgia and the Lady Lucrezia are
enough to drive religion from the world. They make us long to go
back to the traditions of our Roman fathers,—who were men of
cleanly and honorable lives and of heroic deeds, scorning bribery
and deceit. They honored God by noble lives, little as they knew of
Him. But these men are a shame to the mothers that bore them."
"You speak too truly, my son," said the monk. "Alas! the creation
groaneth and travaileth in pain with these things. Many a time and
oft have I seen our master groaning and wrestling with God on this
account. For it is to small purpose that we have gone through Italy
preaching and stirring up the people to more holy lives, when from
the very hill of Zion, the height of the sanctuary, come down these
streams of pollution. It seems as if the time had come that the world
could bear it no longer."
"Well, if it come to the trial of the sword, as come it must," said
the cavalier, "say to your master that Agostino Sarelli has a band of
one hundred tried men and an impregnable fastness in the
mountains, where he may take refuge, and where they will gladly
hear the Word of God from pure lips. They call us robbers,—us who
have gone out from the assembly of robbers, that we might lead
honest and cleanly lives. There is not one among us that hath not
lost houses, lands, brothers, parents, children, or friends through
their treacherous cruelty. There be those whose wives and sisters
have been forced into the Borgia harem; there be those whose
children have been tortured before their eyes,—those who have seen
the fairest and dearest slaughtered by these hell-hounds, who yet sit
in the seat of the Lord and give decrees in the name of Christ. Is
there a God? If there be, why is He silent?"
"Yea, my son, there is a God," said the monk; "but His ways are
not as ours. A thousand years in His sight are but as yesterday, as a
watch in the night. He shall come, and shall not keep silence."
"Perhaps you do not know, father," said the young man, "that I,
too, am excommunicated. I am excommunicated, because, Cæsar
Borgia having killed my oldest brother, and dishonored and slain my
sister, and seized on all our possessions, and the Pope having
protected and confirmed him therein, I declare the Pope to be not of
God, but of the Devil. I will not submit to him, nor be ruled by him;
and I and my fellows will make good our mountains against him and
his crew with such right arms as the good Lord hath given us."
"The Lord be with you, my son!" said the monk; "and the Lord
bring His Church out of these deep waters! Surely, it is a lovely and
beautiful Church, made dear and precious by innumerable saints and
martyrs who have given their sweet lives up willingly for it; and it is
full of records of righteousness, of prayers and alms and works of
mercy that have made even the very dust of our Italy precious and
holy. Why hast Thou abandoned this vine of Thy planting, O Lord?
The boar out of the wood doth waste it; the wild beast of the field
doth devour it. Return, we beseech Thee, and visit this vine of Thy
planting!"
The monk clasped his hands and looked upward pleadingly, the
tears running down his wasted cheeks. Ah, many such strivings and
prayers in those days went up from silent hearts in obscure
solitudes, that wrestled and groaned under that mighty burden
which Luther at last received strength to heave from the heart of the
Church.
"Then, father, you do admit that one may be banned by the
Pope, and may utterly refuse and disown him, and yet be a
Christian?"
"How can I otherwise?" said the monk. "Do I not see the greatest
saint this age or any age has ever seen under the excommunication
of the greatest sinner? Only, my son, let me warn you. Become not
irreverent to the true Church, because of a false usurper. Reverence
the sacraments, the hymns, the prayers all the more for this sad
condition in which you stand. What teacher is more faithful in these
respects than my master? Who hath more zeal for our blessed Lord
Jesus, and a more living faith in Him? Who hath a more filial love
and tenderness towards our blessed Mother? Who hath more
reverent communion with all the saints than he? Truly, he sometimes
seems to me to walk encompassed by all the armies of heaven,—
such a power goes forth in his words, and such a holiness in his life."
"Ah," said Agostino, "would I had such a confessor! The
sacraments might once more have power for me, and I might
cleanse my soul from unbelief."
"Dear son," said the monk, "accept a most unworthy, but sincere
follower of this holy prophet, who yearns for thy salvation. Let me
have the happiness of granting to thee the sacraments of the
Church, which, doubtless, are thine by right as one of the flock of
the Lord Jesus. Come to me some day this week in confession, and
thereafter thou shalt receive the Lord within thee, and be once more
united to Him."
"My good father," said the young man, grasping his hand, and
much affected, "I will come. Your words have done me good; but I
must think more of them. I will come soon; but these things cannot
be done without pondering; it will take some time to bring my heart
into charity with all men."
The monk rose up to depart, and began to gather up his
drawings.
"For this matter, father," said the cavalier, throwing several gold
pieces upon the table, "take these, and as many more as you need
ask for your good work. I would willingly pay any sum," he added,
while a faint blush rose to his cheek, "if you would give me a copy of
this. Gold would be nothing in comparison with it."
"My son," said the monk, smiling, "would it be to thee an image
of an earthly or a heavenly love?"
"Of both, father," said the young man. "For that dear face has
been more to me than prayer or hymn; it has been even as a
sacrament to me, and through it I know not what of holy and
heavenly influences have come to me."
"Said I not well," said the monk, exulting, "that there were those
on whom our Mother shed such grace that their very beauty led
heavenward? Such are they whom the artist looks for, when he
would adorn a shrine where the faithful shall worship. Well, my son,
I must use my poor art for you; and as for gold, we of our convent
take it not except for the adorning of holy things, such as this
shrine."
"How soon shall it be done?" said the young man, eagerly.
"Patience, patience, my Lord! Rome was not built in a day, and
our art must work by slow touches; but I will do my best. But
wherefore, my Lord, cherish this image?"
"Father, are you of near kin to this maid?"
"I am her grandmother's only brother."
"Then I say to you, as the nearest of her male kin, that I seek
this maid in pure and honorable marriage; and she hath given me
her promise, that, if ever she be wife of mortal man, she will be
mine."
"But she looks not to be wife of any man," said the monk; "so, at
least, I have heard her say; though her grandmother would fain
marry her to a husband of her choosing. 'T is a willful woman, is my
sister Elsie, and a worldly,—not easy to persuade, and impossible to
drive."
"And she hath chosen for this fair angel some base peasant churl
who will have no sense of her exceeding loveliness? By the saints, if
it come to this, I will carry her away with the strong arm!"
"That is not to be apprehended just at present. Sister Elsie is
dotingly fond of the girl, which hath slept in her bosom since
infancy."
"And why should I not demand her in marriage of your sister?"
said the young man.
"My Lord, you are an excommunicated man, and she would have
horror of you. It is impossible; it would not be to edification to make
the common people judges in such matters. It is safest to let their
faith rest undisturbed, and that they be not taught to despise
ecclesiastical censures. This could not be explained to Elsie; she
would drive you from her doors with her distaff, and you would
scarce wish to put your sword against it. Besides, my Lord, if you
were not excommunicated, you are of noble blood, and this alone
would be a fatal objection with my sister, who hath sworn on the
holy cross that Agnes shall never love one of your race."
"What is the cause of this hatred?"
"Some foul wrong which a noble did her mother," said the monk;
"for Agnes is of gentle blood on her father's side."
"I might have known it," said the cavalier to himself; "her words
and ways are unlike anything in her class. Father," he added,
touching his sword, "we soldiers are fond of cutting all Gordian
knots, whether of love or religion, with this. The sword, father, is the
best theologian, the best casuist. The sword rights wrongs and
punishes evil doers, and some day the sword may cut the way out of
this embarrass also."
"Gently, my son! gently!" said the monk; "nothing is lost by
patience. See how long it takes the good Lord to make a fair flower
out of a little seed; and He does all quietly, without bluster. Wait on
Him a little in peacefulness and prayer, and see what He will do for
thee."
"Perhaps you are right, my father," said the cavalier, cordially.
"Your counsels have done me good, and I shall seek them further.
But do not let them terrify my poor Agnes with dreadful stories of
the excommunication that hath befallen me. The dear saint is
breaking her good little heart for my sins, and her confessor
evidently hath forbidden her to speak to me or look at me. If her
heart were left to itself, it would fly to me like a little tame bird, and
I would cherish it forever; but now she sees sin in every innocent,
womanly thought,—poor little dear child-angel that she is!"
"Her confessor is a Franciscan," said the monk, who, good as he
was, could not escape entirely from the ruling prejudice of his order,
"and from what I know of him, I should think might be unskillful in
what pertaineth to the nursing of so delicate a lamb. It is not every
one to whom is given the gift of rightly directing souls."
"I'd like to carry her off from him!" said the cavalier, between his
teeth. "I will, too, if he is not careful!" Then he added aloud, "Father,
Agnes is mine,—mine by the right of the truest worship and devotion
that man could ever pay to woman,—mine because she loves me.
For I know she loves me; I know it far better than she knows it
herself, the dear, innocent child! and I will not have her torn from
me to waste her life in a lonely, barren convent, or to be the wife of
a stolid peasant. I am a man of my word, and I will vindicate my
right to her in the face of God and man."
"Well, well, my son, as I said before, patience,—one thing at a
time. Let us say our prayers and sleep to-night, to begin with, and
to-morrow will bring us fresh counsel."
"Well, my father, you will be for me in this matter?" said the
young man.
"My son, I wish you all happiness; and if this be for your best
good and that of my dear niece, I wish it. But, as I said, there must
be time and patience. The way must be made clear. I will see how
the case stands; and you may be sure, when I can in good
conscience, I will befriend you."
"Thank you, my father, thank you!" said the young man, bending
his knee to receive the monk's parting benediction.
"It seems to me not best," said the monk, turning once more, as
he was leaving the threshold, "that you should come to me at
present where I am,—it would only raise a storm that I could not
allay; and so great would be the power of the forces they might
bring to bear on the child, that her little heart might break and the
saints claim her too soon."
"Well, then, father, come hither to me to-morrow at this same
hour, if I be not too unworthy of your pastoral care."
"I shall be too happy, my son," said the monk. "So be it."
And he turned from the door just as the bell of the cathedral
struck the Ave Maria, and all in the street bowed in the evening act
of worship.
CHAPTER XIV
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com