aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/docs/hook_system.html
blob: fbc8baaf758826f8cf3893f7c393cb47a42becec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en" xml:lang="en">
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-language" content="en" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="resource-type" content="document" />
<meta name="distribution" content="global" />
<meta name="copyright" content="phpBB Group" />
<meta name="keywords" content="" />
<meta name="description" content="Hook System explanation" />
<title>phpBB3 &bull; Hook System</title>

<style type="text/css">
/* <![CDATA[ */

/*
	The original "prosilver" theme for phpBB3
	Created by subBlue design :: http://www.subBlue.com
*/

* { margin: 0; padding: 0; }

html { font-size: 100%; height: 100%; margin-bottom: 1px; }

body {
	font-family: Verdana, Helvetica, Arial, sans-serif;
	color: #828282;
	background-color: #FFFFFF;
	font-size: 12px;
	margin: 0;
	padding: 12px 0;
}

img { border-width: 0; }

p {
	line-height: 1.3em;
	font-size: 1.1em;
	margin-bottom: 1.5em;
}

hr {
	border: 0 none #FFFFFF;
	border-top: 1px solid #CCCCCC;
	height: 1px;
	margin: 5px 0;
	display: block;
	clear: both;
}

html, body {
	color: #536482;
	background-color: #FFFFFF;
}

#doc-description h1 {
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	margin-right: 200px;
	color: #FFFFFF;
	margin-top: 15px;
	font-weight: bold;
	font-size: 2em;
	color: #fff;
}

h1 {
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	font-weight: normal;
	color: #000;
	font-size: 2em;
	margin: 0.8em 0 0.2em 0;
}

h2 {
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	font-weight: normal;
	color: #28313F;
	font-size: 1.5em;
	margin: 0.8em 0 0.2em 0;
}

h3 {
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
	border-bottom: 1px solid #CCCCCC;
	margin-bottom: 3px;
	padding-bottom: 2px;
	font-size: 1.05em;
	color: #115098;
	margin-top: 20px;
}

.good { color: green; }
.bad { color: red; }

.version {
	margin-top: 20px;
	text-align: left;
	font-size: 70%;
	color: #006600;
	border-top: 1px solid #ccc;
}

code {
	color: #006600;
	font-weight: normal;
	font-family: 'Courier New', monospace;
	border-color: #D1D7DC;
	border-width: 1px;
	border-style: solid;
	background-color: #FAFAFA;
}

#wrap {
	padding: 0 20px;
	min-width: 650px;
}

#simple-wrap {
	padding: 6px 10px;
}

#page-body {
	margin: 4px 0;
	clear: both;
}

#page-footer {
	clear: both;
}

#logo {
	float: left;
	width: auto;
	padding: 10px 13px 0 10px;
}

a#logo:hover {
	text-decoration: none;
}

#doc-description {
	float: left;
	width: 70%;
}

#doc-description h1 {
	margin-right: 0;
}

.headerbar {
	background: #ebebeb none repeat-x 0 0;
	color: #FFFFFF;
	margin-bottom: 4px;
	padding: 0 5px;
}

span.corners-top, span.corners-bottom, span.corners-top span, span.corners-bottom span {
	font-size: 1px;
	line-height: 1px;
	display: block;
	height: 5px;
	background-repeat: no-repeat;
}

span.corners-top {
	background-image: none;
	background-position: 0 0;
	margin: 0 -5px;
}

span.corners-top span {
	background-image: none;
	background-position: 100% 0;
}

span.corners-bottom {
	background-image: none;
	background-position: 0 100%;
	margin: 0 -5px;
	clear: both;
}

span.corners-bottom span {
	background-image: none;
	background-position: 100% 100%;
}

.paragraph {
	padding: 0 10px;
	margin-bottom: 4px;
	background-repeat: no-repeat;
	background-position: 100% 0;
	background-color: #ECF3F7;
}

.paragraph:target .content {
	color: #000000;
}

.paragraph:target h3 a {
	color: #000000;
}

.content {
	color: #333333;
}

.content h2, .panel h2 {
	color: #115098;
	border-bottom-color:  #CCCCCC;
}

a:link	{ color: #898989; text-decoration: none; }
a:visited	{ color: #898989; text-decoration: none; }
a:hover	{ color: #d3d3d3; text-decoration: underline; }
a:active	{ color: #d2d2d2; text-decoration: none; }

hr {
	border-color: #FFFFFF;
	border-top-color: #CCCCCC;
}

.menu {
	background-color: #cadceb;
}

.headerbar {
	background-color: #12A3EB;
	background-image: url("bg_header.gif");
	color: #FFFFFF;
}

.panel {
	background-color: #ECF1F3;
	color: #28313F;
}


span.corners-top {
	background-image: url("corners_left.png");
}

span.corners-top span {
	background-image: url("corners_right.png");
}

span.corners-bottom {
	background-image: url("corners_left.png");
}

span.corners-bottom span {
	background-image: url("corners_right.png");
}

.error {
	color: #BC2A4D;
}

a:link	{ color: #105289; }
a:visited	{ color: #105289; }
a:hover	{ color: #D31141; }
a:active	{ color: #368AD2; }

.paragraph span.corners-top, .paragraph span.corners-bottom {
	margin: 0 -10px;
}

.content {
	padding: 0;
	line-height: 1.48em;
	color: #333333;
}

.content h2, .panel h2 {
	color: #115098;
	border-bottom-color:  #CCCCCC;
}

.notice {
	border-top-color:  #CCCCCC;
}

.codebox {
	padding: 3px;
	background-color: #FFFFFF;
	border: 1px solid #C9D2D8;
	font-size: 1em;
	margin-bottom: 10px;
	display: block;
	font: 0.9em Monaco, "Andale Mono","Courier New", Courier, mono;
	line-height: 1.3em;
}

* html hr { margin: 0; }
* html span.corners-top, * html span.corners-bottom { background-image: url("corners_left.gif"); }
* html span.corners-top span, * html span.corners-bottom span { background-image: url("corners_right.gif"); }

.back2top {
	clear: both;
	height: 11px;
	text-align: right;
}

.content ol {
	margin-left: 25px;
}

/* ]]> */
</style>

</head>

<body id="phpbb" class="section-docs">

<div id="wrap">
	<a id="top" name="top" accesskey="t"></a>
	<div id="page-header">
		<div class="headerbar">
			<div class="inner"><span class="corners-top"><span></span></span>

			<div id="doc-description">
				<a href="../index.php" id="logo"><img src="site_logo.gif" alt="" /></a>
				<h1>Hook System</h1>
				<p>This is an explanation of how to use the phpBB3 hook system.</p>
				<p style="display: none;"><a href="#start_here">Skip</a></p>
			</div>

			<span class="corners-bottom"><span></span></span></div>
		</div>
	</div>

	<a name="start_here"></a>

	<div id="page-body">

	<h1>Hook System</h1>

	<div class="paragraph menu">
		<div class="inner"><span class="corners-top"><span></span></span>

		<div class="content">

			<ol>
				<li><a href="#intro">Introduction</a></li>
				<li><a href="#use">Allow hooks in functions/methods</a></li>
				<li><a href="#register">Registering hooks</a></li>
				<li><a href="#return">Result returning</a></li>
				<li><a href="#embed">Embedding your hook files/classes/methods</a></li>
				<li><a href="#disclaimer">Copyright and disclaimer</a></li>
			</ol>

		</div>

		<span class="corners-bottom"><span></span></span></div>
	</div>

	<hr />

	<a name="intro"></a><h2>1. Introduction</h2>

	<div class="paragraph">
		<div class="inner"><span class="corners-top"><span></span></span>

		<div class="content">

<h3>What is it?</h3>

<p>The hook system allows applicaton and mod developers to hook into phpBB's or their own functions.</p>

<h3>Pre-defined hookable phpBB3 functions</h3>

<p>In phpBB3 there are four functions you are able to hook into with your custom functions:</p>

<p><code>phpbb_user_session_handler();</code> which is called within user::setup after the session and the user object is correctly initialized.<br />
<code>append_sid($url, $params = false, $is_amp = true, $session_id = false);</code> which is called for building urls (appending the session id)<br />
<code>$template-&gt;display($handle, $include_once = true);</code> which is called directly before outputting the (not-yet-compiled) template.<br />
<code>exit_handler();</code> which is called at the very end of phpBB3's execution.</p>

<p>Please note: The <code>$template-&gt;display</code> hook takes a third <code>$template</code> argument, which is the template instance being used, which should be used instead of the global.</p>

<p>There are also valid external constants you may want to use if you embed phpBB3 into your application:</p>

<div class="codebox"><pre>
PHPBB_MSG_HANDLER (overwrite message handler)
PHPBB_DB_NEW_LINK (overwrite new_link parameter for sql_connect)
PHPBB_ROOT_PATH   (overwrite $phpbb_root_path)
PHPBB_ADMIN_PATH  (overwrite $phpbb_admin_path)
PHPBB_USE_BOARD_URL_PATH (use generate_board_url() for image paths instead of $phpbb_root_path)
</pre></div>

<p>If the <code>PHPBB_USE_BOARD_URL_PATH</code> constant is set to true, phpBB uses generate_board_url() (this will return the boards url with the script path included) on all instances where web-accessible images are loaded. The exact locations are:</p>

<ul>
	<li>/includes/session.php - user::img()</li>
	<li>/includes/functions_content.php - smiley_text()</li>
</ul>

<p>Path locations for the following template variables are affected by this too:</p>

<ul>
	<li>{T_THEME_PATH} - styles/xxx/theme</li>
	<li>{T_TEMPLATE_PATH} - styles/xxx/template</li>
	<li>{T_SUPER_TEMPLATE_PATH} - styles/xxx/template</li>
	<li>{T_IMAGESET_PATH} - styles/xxx/imageset</li>
	<li>{T_IMAGESET_LANG_PATH} - styles/xxx/imageset/yy</li>
	<li>{T_IMAGES_PATH} - images/</li>
	<li>{T_SMILIES_PATH} - $config['smilies_path']/</li>
	<li>{T_AVATAR_PATH} - $config['avatar_path']/</li>
	<li>{T_AVATAR_GALLERY_PATH} - $config['avatar_gallery_path']/</li>
	<li>{T_ICONS_PATH} - $config['icons_path']/</li>
	<li>{T_RANKS_PATH} - $config['ranks_path']/</li>
	<li>{T_UPLOAD_PATH} - $config['upload_path']/</li>
	<li>{T_STYLESHEET_LINK} - styles/xxx/theme/stylesheet.css (or link to style.php if css is parsed dynamically)</li>
	<li>New template variable {BOARD_URL} for the board url + script path.</li>
</ul>


		</div>

		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>

		<span class="corners-bottom"><span></span></span></div>
	</div>

	<a name="use"></a><h2>2. Allow hooks in functions/methods</h2>

	<div class="paragraph">
		<div class="inner"><span class="corners-top"><span></span></span>

		<div class="content">

<p>The following examples explain how phpBB3 utilize the in-build hook system. You will be more interested in registering your hooks, but showing you this may help you understand the system better along the way.</p>

<p>First of all, this is how a function need to be layed out if you want to allow it to be hookable...</p>

<div class="codebox"><pre>
function my_own_function($my_first_parameter, $my_second_parameter)
{
	global $phpbb_hook;

	if ($phpbb_hook-&gt;call_hook(__FUNCTION__, $my_first_parameter, $my_second_parameter))
	{
		if ($phpbb_hook-&gt;hook_return(__FUNCTION__))
		{
			return $phpbb_hook-&gt;hook_return_result(__FUNCTION__);
		}
	}

	[YOUR CODE HERE]
}
</pre></div>

<p>Above, the call_hook function should always be mapping your function call... in regard to the number of parameters passed.</p>

<p>This is how you could make a method being hookable...</p>

<div class="codebox"><pre>
class my_hookable_object
{
	function hook_me($my_first_parameter, $my_second_parameter)
	{
		global $phpbb_hook;

		if ($phpbb_hook-&gt;call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
		{
			if ($phpbb_hook-&gt;hook_return(array(__CLASS__, __FUNCTION__)))
			{
				return $phpbb_hook-&gt;hook_return_result(array(__CLASS__, __FUNCTION__));
			}
		}

		[YOUR CODE HERE]
	}
}
</pre></div>

<p>The only difference about calling it is the way you define the first parameter. For a function it is only <code>__FUNCTION__</code>, for a method it is <code>array(__CLASS__, __FUNCTION__)</code>. In PHP4 __CLASS__ is always returning the class in lowercase.</p>

<p>Now, in phpBB there are some pre-defined hooks available, but how do you make your own hookable function available (and therefore allowing others to hook into it)? For this, there is the add_hook() method:</p>

<div class="codebox"><pre>
// Adding your own hookable function:
$phpbb_hook-&gt;add_hook('my_own_function');

// Adding your own hookable method:
$phpbb_hook-&gt;add_hook(array('my_hookable_object', 'hook_me'));
</pre></div>

<p>You are also able to remove the possibility of hooking a function/method by calling <code>$phpbb_hook-&gt;remove_hook()</code> with the same parameters as add_hook().<br />
This comes in handy if you want to force some hooks not to be called - at all.</p>

		</div>

		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>

		<span class="corners-bottom"><span></span></span></div>
	</div>

	<a name="register"></a><h2>3. Registering hooks</h2>

	<div class="paragraph">
		<div class="inner"><span class="corners-top"><span></span></span>

		<div class="content">

		<h3>Registering hooks</h3>

<p>Now to actually defining your functions which should be called. For this we take the append_sid() function as an example (this function is able to be hooked by default). We create two classes, one being static and a function:</p>

<div class="codebox"><pre>
class my_append_sid_class
{
	// Our functions
	function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
	{
		// Get possible previous results
		$result = $hook-&gt;previous_hook_result('append_sid');

		return $result['result'] . '&lt;br /&gt;And i was the second one.';
	}
}

// Yet another class :o
class my_second_append_sid_class
{
	function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
	{
		// Get possible previous results
		$result = $hook-&gt;previous_hook_result('append_sid');

		echo $result['result'] . '&lt;br /&gt;I was called as the third one.';
	}
}

// And a normal function
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
	// Get possible previous results
	$result = $hook-&gt;previous_hook_result('append_sid');

	return 'I was called as the first one';
}

// Initializing the second class
$my_second_append_sid_class = new my_second_append_sid_class();
</pre></div>

<p>Make sure you add the same parameters to your function as is defined for the hookable function with one exception: The first variable is always <code>&amp;$hook</code>... this is the hook object itself you are able to operate on.</p>

<p>Now we register the hooks one by one with the <code>$phpbb_hook-&gt;register()</code> method:</p>

<div class="codebox"><pre>
// Now, we register our append_sid &quot;replacements&quot; in a stacked way...
// Registering the function (this is called first)
$phpbb_hook-&gt;register('append_sid', 'my_append_sid');

// Registering the first class
$phpbb_hook-&gt;register('append_sid', array('my_append_sid_class', 'my_append_sid'));
$phpbb_hook-&gt;register('append_sid', array(&amp;$my_second_append_sid_class, 'my_append_sid'));
</pre></div>

<p>With this you are even able to make your own functions that are already hooked itself being hooked again...</p>

<div class="codebox"><pre>
// Registering hook, which will be called
$phpbb_hook-&gt;register('append_sid', 'my_own_append_sid');

// Add hook to our called hook function
$phpbb_hook-&gt;add_hook('my_own_append_sid');

// Register added hook
$phpbb_hook-&gt;register('my_own_append_sid', 'also_my_own_append_sid');
</pre></div>

		<h3>Special treatment/chains</h3>

		<p>The <code>register</code> method is able to take a third argument to specify a special 'chain' mode. The valid modes are <code>first</code>, <code>last</code> and <code>standalone</code></p>

		<p><code>$phpbb_hook-&gt;register('append_sid', 'my_own_append_sid', 'first')</code> would make sure that the function is called in the beginning of the chain. It is possible that more than one function is called within the first block - here the FIFO principle is used.</p>

		<p><code>$phpbb_hook-&gt;register('append_sid', 'my_own_append_sid', 'last')</code> would make sure that the function is called at the very end of the chain. It is possible that more than one function is called within the last block - here the FIFO principle is used.</p>

		<p><code>$phpbb_hook-&gt;register('append_sid', 'my_own_append_sid', 'standalone')</code> makes sure only the defined function is called. All other functions are removed from the chain and no other functions are added to it later on. If two applications try to trigger the standalone mode a PHP notice will be printed and the second function being discarded.</p>

		<h3>Only allowing hooks for some objects</h3>

		<p>Because the hook system is not able to differate between initialized objects and only operate on the class, you need to solve this on the code level.</p>

		<p>One possibility would be to use a property:</p>

		<div class="codebox"><pre>
class my_hookable_object
{
	function blabla()
	{
	}
}

class my_hookable_object2 extends my_hookable_object
{
	var $call_hook = true;

	function hook_me($my_first_parameter, $my_second_parameter)
	{
		if ($this-&gt;call_hook)
		{
			global $phpbb_hook;

			if ($phpbb_hook-&gt;call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
			{
				if ($phpbb_hook-&gt;hook_return(array(__CLASS__, __FUNCTION__)))
				{
					return $phpbb_hook-&gt;hook_return_result(array(__CLASS__, __FUNCTION__));
				}
			}
		}

		return 'not hooked';
	}
}

function hooking(&amp;$hook, $first, $second)
{
	return 'hooked';
}

$first_object = new my_hookable_object2();
$second_object = new my_hookable_object2();

$phpbb_hook-&gt;add_hook(array('my_hookable_object2', 'hook_me'));

$phpbb_hook-&gt;register(array('my_hookable_object2', 'hook_me'), 'hooking');

// Do not call the hook for $first_object
$first_object-&gt;call_hook = false;

echo $first_object-&gt;hook_me('first', 'second') . '&lt;br /&gt;';
echo $second_object-&gt;hook_me('first', 'second') . '&lt;br /&gt;';
</pre></div>

<p>OUTPUT:</p>

<div class="codebox"><pre>
not hooked
hooked
</pre></div>

		<p>A different possibility would be using a function variable (which could be left out on passing the function variables to the hook):</p>

		<div class="codebox"><pre>
class my_hookable_object
{
	function blabla()
	{
	}
}

class my_hookable_object2 extends my_hookable_object
{
	function hook_me($my_first_parameter, $my_second_parameter, $hook_me = true)
	{
		if ($hook_me)
		{
			global $phpbb_hook;

			if ($phpbb_hook-&gt;call_hook(array(__CLASS__, __FUNCTION__), $my_first_parameter, $my_second_parameter))
			{
				if ($phpbb_hook-&gt;hook_return(array(__CLASS__, __FUNCTION__)))
				{
					return $phpbb_hook-&gt;hook_return_result(array(__CLASS__, __FUNCTION__));
				}
			}
		}

		return 'not hooked';
	}
}

function hooking(&amp;$hook, $first, $second)
{
	return 'hooked';
}

$first_object = new my_hookable_object2();
$second_object = new my_hookable_object2();

$phpbb_hook-&gt;add_hook(array('my_hookable_object2', 'hook_me'));

$phpbb_hook-&gt;register(array('my_hookable_object2', 'hook_me'), 'hooking');

echo $first_object-&gt;hook_me('first', 'second', false) . '&lt;br /&gt;';
echo $second_object-&gt;hook_me('first', 'second') . '&lt;br /&gt;';
		</pre></div>

		<p>OUTPUT:</p>

		<div class="codebox"><pre>
not hooked
hooked
		</pre></div>

		</div>

		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>

		<span class="corners-bottom"><span></span></span></div>
	</div>

	<a name="return"></a><h2>4. Result returning</h2>

	<div class="paragraph">
		<div class="inner"><span class="corners-top"><span></span></span>

		<div class="content">

<p>Generally, the distinction has to be made if a function returns the result obtained from the called function or continue the execution. Based on the needs of the application this may differ. Therefore, the function returns the results only if the called hook function is returning a result.</p>

<h3>Case 1 - Returning the result</h3>

<p>Imagine the following function supporting hooks:</p>

<div class="codebox"><pre>
function append_sid($url, $params = false, $is_amp = true, $session_id = false)
{
	global $_SID, $_EXTRA_URL, $phpbb_hook;

	// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropiatly.
	// They could mimick most of what is within this function
	if ($phpbb_hook-&gt;call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
	{
		if ($phpbb_hook-&gt;hook_return(__FUNCTION__))
		{
			return $phpbb_hook-&gt;hook_return_result(__FUNCTION__);
		}
	}

	[...]
}
</pre></div>

<p>Now, the following function is yours. Since you return a value, the append_sid() function itself is returning it as is:</p>

<div class="codebox"><pre>
// The function called
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
	// Get possible previous results
	$result = $hook-&gt;previous_hook_result('append_sid');

	return 'Since i return something the append_sid() function will return my result.';
}
</pre></div>

<p>To be able to get the results returned from functions higher in the change the <code>previous_hook_result()</code> method should always be used, it returns an <code>array('result' => [your result])</code> construct.</p>

<h3>Case 2 - Not Returning any result</h3>

<p>Sometimes applications want to return nothing and therefore force the underlying function to continue it's execution:</p>

<div class="codebox"><pre>
function append_sid($url, $params = false, $is_amp = true, $session_id = false)
{
	global $_SID, $_EXTRA_URL, $phpbb_hook;

	// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropiatly.
	// They could mimick most of what is within this function
	if ($phpbb_hook-&gt;call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
	{
		if ($phpbb_hook-&gt;hook_return(__FUNCTION__))
		{
			return $phpbb_hook-&gt;hook_return_result(__FUNCTION__);
		}
	}

	[...]
}

// The function called
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
	// Get possible previous results
	$result = $hook-&gt;previous_hook_result('append_sid');

	[...]

	// I only rewrite some variables, but return nothing. Therefore, the append_sid() function will not return my (non)result.
}
</pre></div>

<p>Please Note: The decision to return or not return is solely made of the very last function call within the hook chain. An example:</p>

<div class="codebox"><pre>
// The function called
function my_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
	// Get possible previous results
	$result = $hook-&gt;previous_hook_result('append_sid');

	// $result is not filled

	return 'FILLED';
}

// This function is registered too and gets executed after my_append_sid()
function my_own_append_sid(&amp;$hook, $url, $params = false, $is_amp = true, $session_id = false)
{
	$result = $hook->previous_hook_result('append_sid');

	// $result is actually filled with $result['result'] = 'FILLED'
	// But i return nothing, therefore append_sid() continues it's execution.
}

// The way both functions are registered.
$phpbb_hook->register('append_sid', 'my_append_sid');
$phpbb_hook->register('append_sid', 'my_own_append_sid');
</pre></div>

		</div>

		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>

		<span class="corners-bottom"><span></span></span></div>
	</div>

	<a name="embed"></a><h2>5. Embedding your hook files/classes/methods</h2>

	<div class="paragraph">
		<div class="inner"><span class="corners-top"><span></span></span>

		<div class="content">

<p>There are basically two methods you are able to choose from:</p>

<p>1) Add a file to includes/hooks/. The file need to be prefixed by <code>hook_</code>. This file is included within common.php, you are able to register your hooks, include other files or functions, etc. It is advised to only include other files if needed (within a function call for example).</p>

<p>Please be aware that you need to purge your cache within the ACP to make your newly placed file available to phpBB3.</p>

<p>2) The second method is meant for those wanting to wrap phpBB3 without placing a custom file to the hooks directory. This is mostly done by including phpBB's files within the application file. To be able to register your hooks you need to create a function within your application:</p>

<div class="codebox"><pre>
// My function which gets executed within the hooks constuctor
function phpbb_hook_register(&amp;$hook)
{
	$hook-&gt;register('append_sid', 'my_append_sid');
}

[...]
</pre></div>

<p>You should get the idea. ;)</p>

		</div>

		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>

		<span class="corners-bottom"><span></span></span></div>
	</div>

	<a name="disclaimer"></a><h2>6. Copyright and disclaimer</h2>

	<div class="paragraph">
		<div class="inner"><span class="corners-top"><span></span></span>

		<div class="content">

	<p>This application is opensource software released under the <a href="http://opensource.org/licenses/gpl-2.0.php">GNU General Public License v2</a>. Please see source code and the docs directory for more details. This package and its contents are Copyright (c) <a href="https://www.phpbb.com/">phpBB Group</a>, All Rights Reserved.</p>

		</div>

		<div class="back2top"><a href="#wrap" class="top">Back to Top</a></div>

		<span class="corners-bottom"><span></span></span></div>
	</div>

	<div id="page-footer">
		<div class="version">&nbsp;</div>
	</div>
</div></div>

<div>
	<a id="bottom" name="bottom" accesskey="z"></a>
</div>

</body>
</html>