<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>&#8235;בנית אתרים&#8236;</title>	<atom:link href="http://www.xn----0hcb6acyq1epc.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xn----0hcb6acyq1epc.com</link>
	<description>&#8235;בנית-אתרים.com - בלוג בנית אתרים&#8236;</description>	<lastBuildDate>Mon, 03 Jan 2011 10:33:44 +0000</lastBuildDate>
	<language>he</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>&#8235;MySQL: השוואה LIKE נגד REGEXP&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/mysql-%d7%94%d7%a9%d7%95%d7%95%d7%90%d7%94-like-%d7%a0%d7%92%d7%93-regexp/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/mysql-%d7%94%d7%a9%d7%95%d7%95%d7%90%d7%94-like-%d7%a0%d7%92%d7%93-regexp/#comments</comments>
		<pubDate>Sun, 02 Jan 2011 09:37:41 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[טיפים והערות]]></category>
		<category><![CDATA[יעילות]]></category>
		<category><![CDATA[שפות תכנות]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[ביצועים]]></category>
		<category><![CDATA[טיפים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=475</guid>
		<description><![CDATA[&#8235;בMySQL ישנן שתי פעולות מעניינות לחיפוש במחרוזות. הראשונה היא LIKE והשניה היא REGEXP. לא אפרט וארחיב יותר מדי עליהן, את תפקודן המלא ניתן לקרוא באתר של MySQL להרחבה. LIKE מאפשרת לנו להשתמש בסימן _ בכדי לתפוס כל תו שהוא וב% בכדי לתפוס כל רצף שהוא של תווים (כולל 0). REGEXP מאפשרת לנו להריץ ביטויים רגולוריים. [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p>בMySQL ישנן שתי פעולות מעניינות לחיפוש במחרוזות. הראשונה היא LIKE והשניה היא REGEXP. לא אפרט וארחיב יותר מדי עליהן, את תפקודן המלא ניתן <a href="http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html" target="_blank">לקרוא באתר של MySQL להרחבה</a>.<br />
LIKE מאפשרת לנו להשתמש בסימן _ בכדי לתפוס כל תו שהוא וב% בכדי לתפוס כל רצף שהוא של תווים (כולל 0). REGEXP מאפשרת לנו להריץ ביטויים רגולוריים.<br />
שימוש נפוץ לפעולות האלו הוא חיפוש בתוך מחרוזת. במאמר אבדוק את היעילות של כל אחת מהן, בבדיקה של האם מחרוזת מכילה תת מחרוזת, תפקיד ששתיהן תבצענה בקלות ותשגנה את אותה המטרה.</p>
<p>את המבחן אבצע עם שרת MySQL שמותקן לוקאלית על המחשב שלי. אצור טבלה עם עמודה id ועמודה name באורך 32 תווים. אכניס הרבה שורות (100,000) עם מידע שונה (md5 של מונה הלולאה) ואראה איזו מהשיטות יותר יעילה לחיפוש תת המחרוזת 5eda</p>
<p>הקוד שבו השתמשתי ליצירת הטבלאות והכנסת הנתונים הוא:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`tbl`</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	<span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">COLLATE</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'utf8_general_ci'</span>
ENGINE<span style="color: #66cc66;">=</span>MyISAM
ROW_FORMAT<span style="color: #66cc66;">=</span><span style="color: #993333; font-weight: bold;">DEFAULT</span>;
&nbsp;
DELIMITER $$
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> DO_INSERT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #993333; font-weight: bold;">BEGIN</span>
		<span style="color: #993333; font-weight: bold;">DECLARE</span> i <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">0</span>;
		my_loop: LOOP
			<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`tbl`</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>MD5<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #993333; font-weight: bold;">SET</span> i <span style="color: #66cc66;">=</span> i <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
			<span style="color: #993333; font-weight: bold;">IF</span> i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">100000</span> <span style="color: #993333; font-weight: bold;">THEN</span> 
				LEAVE my_loop;
			<span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">IF</span>;
		<span style="color: #993333; font-weight: bold;">END</span> LOOP my_loop;
	<span style="color: #993333; font-weight: bold;">END</span> $$
DELIMITER ;
<span style="color: #993333; font-weight: bold;">CALL</span> DO_INSERT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</div>
<p>הקודים שבהם השתמשתי לשליפת נתונים עם LIKE וREGEXP הם:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`tbl`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%5eda%'</span>;</pre></div></div>

</div>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`tbl`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">REGEXP</span> <span style="color: #ff0000;">'5eda'</span>;</pre></div></div>

</div>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`tbl`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">REGEXP</span> <span style="color: #ff0000;">'^.*5eda.*$'</span>;</pre></div></div>

</div>
<p>התוצאות היו ברורות וחד משמעיות. בעת שימוש בLIKE קיבלתי זמן 0.046 ו0.062 שניות (כנראה בגלל caching), בעת שימוש בREGEXP בשיטה הראשונה קיבלתי זמן 0.125 ו0.144 שניות ובשיטה השנייה 0.328 ו0.344 שניות.<br />
שתי הפעולות השיגו את אותו הדבר בדיוק, רק שLIKE הייתה מהירה פי יותר מ2.5 מREGEXP בשיטה הראשונה ופי 6.25 בשיטה השנייה!</p>
<p>שיהיה לכם שימוש מושכל!</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/mysql-%d7%94%d7%a9%d7%95%d7%95%d7%90%d7%94-like-%d7%a0%d7%92%d7%93-regexp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;אופטימיזציית לולאות for בPHP&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%90%d7%95%d7%a4%d7%98%d7%99%d7%9e%d7%99%d7%96%d7%a6%d7%99%d7%99%d7%aa-%d7%9c%d7%95%d7%9c%d7%90%d7%95%d7%aa-for-%d7%91php/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%90%d7%95%d7%a4%d7%98%d7%99%d7%9e%d7%99%d7%96%d7%a6%d7%99%d7%99%d7%aa-%d7%9c%d7%95%d7%9c%d7%90%d7%95%d7%aa-for-%d7%91php/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 11:00:26 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[טיפים והערות]]></category>
		<category><![CDATA[יעילות]]></category>
		<category><![CDATA[שפות תכנות]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ביצועים]]></category>
		<category><![CDATA[טיפים]]></category>
		<category><![CDATA[תכנות אתרים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=463</guid>
		<description><![CDATA[&#8235;שלום לכם קוראים יקרים. בחנתי 4 דרכים (ששתיים מהן שקולות) שונות לבצע איטרציה על מערך בעזרת לולאות for. שיטות איטרציה unoptimized: for($i = 0; $i &#60; count($array); $i++) { } semioptimized1: $c = count($array); for($i = 0; $i &#60; $c; $i++) { } semioptimized2: for($i = 0, $c = count($array); $i &#60; $c; $i++) { [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p>שלום לכם קוראים יקרים.<br />
בחנתי 4 דרכים (ששתיים מהן שקולות) שונות לבצע איטרציה על מערך בעזרת לולאות for.</p>
<h2>שיטות איטרציה</h2>
<ul style="direction: ltr; text-align: left;">
<li>unoptimized:<br />
for($i = 0; $i &lt; count($array); $i++) { }</li>
<li>semioptimized1:<br />
$c = count($array);<br />
for($i = 0; $i &lt; $c; $i++) { }</li>
<li>semioptimized2:<br />
for($i = 0, $c = count($array); $i &lt; $c; $i++) { }</li>
<li>optimized:<br />
for($i = count($array) &#8211; 1; $i &gt;= 0; $i&#8211;) { }</li>
</ul>
<p>חדי העיין ביניכם יבחחינו כי השיטה השנייה והשלישית שקולות לחלוטין.<br />
הרצתי את הקודים השונים, מדדתי זמנים וצריכת זכרון בתחילת ובסוף הריצה והגעתי לתוצאות. (ניתן <a href="http://www.xn----0hcb6acyq1epc.com/for.rar">להוריד את קבצי הבדיקות והתוצאות</a>)</p>
<h2>תוצאות</h2>
<div style="text-align: left; direction: ltr;">
<h3>array range 0 to 999</h3>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span style="text-decoration: underline;"><strong>type</strong></span></td>
<td><span style="text-decoration: underline;"><strong>time</strong></span></td>
<td><span style="text-decoration: underline;"><strong>memory</strong></span></td>
</tr>
<tr>
<td><strong>unoptimized</strong></td>
<td>0.001812</td>
<td>80</td>
</tr>
<tr>
<td><strong>semioptimized1</strong></td>
<td>0.000217</td>
<td>160</td>
</tr>
<tr>
<td><strong>semioptimized2</strong></td>
<td>0.000216</td>
<td>160</td>
</tr>
<tr>
<td><strong>optimized</strong></td>
<td>0.000208</td>
<td>80</td>
</tr>
</tbody>
</table>
<p><img src="http://i55.tinypic.com/280l3xu.jpg" alt="" width="493" height="297" /></p>
<h3>array range 0 to 99999</h3>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><span style="text-decoration: underline;"><strong>type</strong></span></td>
<td><span style="text-decoration: underline;"><strong>time</strong></span></td>
<td><span style="text-decoration: underline;"><strong>memory</strong></span></td>
</tr>
<tr>
<td><strong>unoptimized</strong></td>
<td>0.12129</td>
<td>80</td>
</tr>
<tr>
<td><strong>semioptimized1</strong></td>
<td>0.01303</td>
<td>160</td>
</tr>
<tr>
<td><strong>semioptimized2</strong></td>
<td>0.01338</td>
<td>160</td>
</tr>
<tr>
<td><strong>optimized</strong></td>
<td>0.01303</td>
<td>80</td>
</tr>
</tbody>
</table>
<p><img src="http://i52.tinypic.com/wly640.jpg" alt="" width="493" height="297" />
</div>
<h3>מסקנות</h3>
<p>המסקנות העולות מהבדיקות הן די ברורות. הקוד הקצר ביותר והפופולארי ביותר הוא הראשון, אך הוא <strong>איטי פי עשרה</strong> משלושת הקודים האחרים!<br />
הדרך הרביעית היא כנראה הדרך הנוחה ביותר הקצרה מבין שלושת הנותרות והיעילה ביותר, אלא אם ישנה חשיבות לסדר הלולאה.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%90%d7%95%d7%a4%d7%98%d7%99%d7%9e%d7%99%d7%96%d7%a6%d7%99%d7%99%d7%aa-%d7%9c%d7%95%d7%9c%d7%90%d7%95%d7%aa-for-%d7%91php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8235;השוואה בין סוגי הכנסה למערך&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%a9%d7%95%d7%95%d7%90%d7%94-%d7%91%d7%99%d7%9f-%d7%a1%d7%95%d7%92%d7%99-%d7%94%d7%9b%d7%a0%d7%a1%d7%94-%d7%9c%d7%9e%d7%a2%d7%a8%d7%9a/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%a9%d7%95%d7%95%d7%90%d7%94-%d7%91%d7%99%d7%9f-%d7%a1%d7%95%d7%92%d7%99-%d7%94%d7%9b%d7%a0%d7%a1%d7%94-%d7%9c%d7%9e%d7%a2%d7%a8%d7%9a/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 11:47:14 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[טיפים והערות]]></category>
		<category><![CDATA[יעילות]]></category>
		<category><![CDATA[שפות תכנות]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ביצועים]]></category>
		<category><![CDATA[טיפים]]></category>
		<category><![CDATA[תכנות אתרים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=449</guid>
		<description><![CDATA[&#8235;שלום לכם קוראים יקרים, בפוסט הזה אדבר על שיטות שונות להכניס נתונים למערך ועל ההבדלים ביניהן מבחינת ביצועים. ראשית אציג את 4 השיטות הנבדקות: $arr[$i] = $val; &#8211; הכנסת הערך $val למערך $arr במקום ה$i. $arr[] = $val; &#8211; הכנסת הערך $val למערך $arr במקום הבא (לדוגמה אם יש אינדקסים 0, 1, 2 המקום יהיה [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p>שלום לכם קוראים יקרים, בפוסט הזה אדבר על שיטות שונות להכניס נתונים למערך ועל ההבדלים ביניהן מבחינת ביצועים.<br />
ראשית אציג את 4 השיטות הנבדקות:</p>
<ul>
<li><strong dir="ltr">$arr[$i] = $val;</strong> &#8211; הכנסת הערך $val למערך $arr במקום ה$i.</li>
<li><strong dir="ltr">$arr[] = $val;</strong> &#8211; הכנסת הערך $val למערך $arr במקום הבא (לדוגמה אם יש אינדקסים 0, 1, 2 המקום יהיה 3).</li>
<li><strong dir="ltr">array_push($arr, $val);</strong> &#8211; זהה לחלוטין לקודם מבחינת תפקוד.</li>
<li>נבחן שיטה אחת נוספת שממלאת מערך במספרים מa עד b נכתב כך: <strong dir="ltr">$arr = range($a, $b);</strong></li>
</ul>
<h3>המבחן</h3>
<p>בניתי מבחן קטן, שמכניס את המספרים מ0 עד 99,999 לתאים המתאימים (0 ב0, 1 ב1 וכו') &#8211; סה&quot;כ 100,000 תאים.<br />
המבחן בנוי מהצהרה על המערך ולולאה שמכניסה את $i למקום ה$i בהתאם לשיטות 1 עד 3 (הניחו כי $val הוא $i).</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'TIME'</span><span style="color: #339933;">,</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// HERE COMES THE ACTION</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">TIME</span><span style="color: #339933;">;</span></pre></div></div>

</pre>
</div>
<p>נוסף לכך הרצתי במקום הלולאה את הקוד הבא:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">99999</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p>התוצאות נראו כך (פחות שניות יותר טוב):</p>
<ul>
<li><span style="text-decoration: underline;" dir="ltr">$arr[$i] = $i;</span><br />
0.046030044555664 (שניות)</li>
<li><span style="text-decoration: underline;" dir="ltr">$arr[] = $i;</span><br />
0.048002958297729 (שניות)</li>
<li><span style="text-decoration: underline;" dir="ltr">array_push($arr, $val);</span><br />
0.17743587493896 (שניות)</li>
<li><span style="text-decoration: underline;" dir="ltr">$arr = range(0, 100000);</span><br />
0.020608901977539(שניות)</li>
</ul>
<p>או בגרף (<strong>כמה שנמוך יותר, יותר מהיר</strong>):<br />
<a href="http://i54.tinypic.com/2roj28p.jpg" target="_blank"><img class="alignnone" src="http://i54.tinypic.com/2roj28p.jpg" alt="" width="338" height="169" /></a></p>
<p>לכן הסקתי את המסקנות הבאות:</p>
<ul>
<li>במידה ואתם צריכים מערך שמכיל מספרים מa עד b תשתמשו בפונקציה range.</li>
<li>עדיף במעט להשתמש באינדקסים (זניח).</li>
<li><strong>לא להשתמש בarray_push היא איטית ביותר מפי 3.5!</strong></li>
</ul>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%a9%d7%95%d7%95%d7%90%d7%94-%d7%91%d7%99%d7%9f-%d7%a1%d7%95%d7%92%d7%99-%d7%94%d7%9b%d7%a0%d7%a1%d7%94-%d7%9c%d7%9e%d7%a2%d7%a8%d7%9a/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8235;על char וvarchar&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%a2%d7%9c-char-%d7%95varchar/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%a2%d7%9c-char-%d7%95varchar/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 22:13:26 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[טיפים והערות]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מדריכי בנית אתרים]]></category>
		<category><![CDATA[DBMS]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[ביצועים]]></category>
		<category><![CDATA[טיפים]]></category>
		<category><![CDATA[מונחים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=443</guid>
		<description><![CDATA[&#8235;סביר להניח שכל מי שאי פעם תכנן database נתקל בשני הטיפוסים char וvarchar, אך בואו ננסה להבין מה ההבדל ביניהם. לכאורה, אם אשתמש בchar(32) או varchar(32) אוכל להכניס מידע באורך של עד 32 בייתים לכל שורה במסד בעמודה הזו. אך מתחת לפני השטח הdatabase משתמש בשניים בצורה שונה. המבנה של char פשוט יותר להסברה: עבור [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p>סביר להניח שכל מי שאי פעם תכנן database נתקל בשני הטיפוסים char וvarchar, אך בואו ננסה להבין מה ההבדל ביניהם.</p>
<p>לכאורה, אם אשתמש בchar(32) או varchar(32) אוכל להכניס מידע באורך של עד 32 בייתים לכל שורה במסד בעמודה הזו.<br />
אך מתחת לפני השטח הdatabase משתמש בשניים בצורה שונה.</p>
<p>המבנה של char פשוט יותר להסברה:<br />
עבור ההצהרה char(X) בכל שורה יוקצו בדיוק X בייתים לאחסון. במידה ונכניס רק X &#8211; Y בייתים, יתווספו ברצף Y בייתים ובהם NULL.<br />
המבנה של varchar טיפה יותר מורכב:<br />
עבור ההצהרה varchar(X) בכל שורה יוקצו עד X + 1 בייתים לאחסון. במידה ויוכנסו Y בייתים, יתפסו Y + 1 בייתים בלבד, שהאחד הנוסף מכיל NULL המציין את סוף המידע.</p>
<p>מתי להשתמש במה:<br />
כאשר הגודל הוא קבוע או כמעט כמובן שעדיף להשתמש בchar, הן פחות מקום יתפס ואם כל השורה היא fixed length (משמע מכילה רק טיפוסים באורך קבוע כמו מספרים, char, enum וכו') גם מעבר על שורות יהיה מהיר יותר.<br />
כאשר הגודל משתנה מומלץ להשתמש בvarchar, כך נחסוך מקום באופן משמעותי.</p>
<p>מקווה שלמדתם משהו חדש!</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%a2%d7%9c-char-%d7%95varchar/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>&#8235;ההבדל בין == ל=== וביצועים&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%94%d7%91%d7%93%d7%9c-%d7%91%d7%99%d7%9f-%d7%9c-%d7%95%d7%91%d7%99%d7%a6%d7%95%d7%a2%d7%99%d7%9d/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%94%d7%91%d7%93%d7%9c-%d7%91%d7%99%d7%9f-%d7%9c-%d7%95%d7%91%d7%99%d7%a6%d7%95%d7%a2%d7%99%d7%9d/#comments</comments>
		<pubDate>Sun, 09 May 2010 20:36:40 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[טיפים והערות]]></category>
		<category><![CDATA[יעילות]]></category>
		<category><![CDATA[שפות תכנות]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ביצועים]]></category>
		<category><![CDATA[טיפים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=435</guid>
		<description><![CDATA[&#8235;בזמן האחרון גיליתי שיש הבדל מאוד מאוד משמעותי בין == ל=== מבחינת ביצועים. ההבדל בגדול מבחינת השימוש הוא ש == משווה את הערך ו=== גם משווה את סוג המשנה. כך שלדוגמה string לא ריק == לtrue אבל לא === לtrue וכדומה. החלטתי לבדוק את העניין ולראות האם זה משתלם להשתמש ב===, לדוגמה כאשר בודקים איזה [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p>בזמן האחרון גיליתי שיש הבדל מאוד מאוד משמעותי בין == ל=== מבחינת ביצועים.<br />
ההבדל בגדול מבחינת השימוש הוא ש == משווה את הערך ו=== גם משווה את סוג המשנה. כך שלדוגמה string לא ריק == לtrue אבל לא === לtrue וכדומה.</p>
<p>החלטתי לבדוק את העניין ולראות האם זה משתלם להשתמש ב===, לדוגמה כאשר בודקים איזה string יש במשנה או משהו כזה.<br />
מסתבר שבהשוואת מחרוזות יש <strong>שיפור של בין 20% ל400% במהירות!</strong><br />
זמן יקר ובהחלט בהחלט לא זניח. ככל שהמחרוזות היו יותר ארוכות כך ההבדלים היו יותר גדולים.<br />
חשוב לציין שלא הצלחתי למצוא חוקיות מוחלטת של מתי זה יעיל מאוד ומתי זה יעיל פחות, אבל בגדול כל עוד אין סיבה לא להשתמש ב=== עדיף להשתמש בו <img src='http://www.xn----0hcb6acyq1epc.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%94%d7%91%d7%93%d7%9c-%d7%91%d7%99%d7%9f-%d7%9c-%d7%95%d7%91%d7%99%d7%a6%d7%95%d7%a2%d7%99%d7%9d/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>&#8235;ביצועים &#8211; פונקציות, OOP וסטטיות&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%91%d7%99%d7%a6%d7%95%d7%a2%d7%99%d7%9d-%d7%a4%d7%95%d7%a0%d7%a7%d7%a6%d7%99%d7%95%d7%aa-oop-%d7%95%d7%a1%d7%98%d7%98%d7%99%d7%95%d7%aa/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%91%d7%99%d7%a6%d7%95%d7%a2%d7%99%d7%9d-%d7%a4%d7%95%d7%a0%d7%a7%d7%a6%d7%99%d7%95%d7%aa-oop-%d7%95%d7%a1%d7%98%d7%98%d7%99%d7%95%d7%aa/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 13:17:02 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[טיפים והערות]]></category>
		<category><![CDATA[יעילות]]></category>
		<category><![CDATA[שפות תכנות]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ביצועים]]></category>
		<category><![CDATA[השוואה]]></category>
		<category><![CDATA[תכנות אתרים]]></category>
		<category><![CDATA[תכנות מונחה עצמים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=432</guid>
		<description><![CDATA[&#8235;החלטתי היום לבצע מבחן, רציתי לבדוק מה מהבאים הכי מהיר ובאיזה הפרש. המועמדים הם: פונקציה פשוטה וסטנדרטית. מחלקה רגילה, יצירת אובייקט וקריאות לפעולה בו. מחלקה עם פעולה סטטית וקריאות ישירות אליה. שימוש בDesign Pattern שנקרא Singleton שבו יוצרים מחלקה רגילה ופעולה סטטית אחת שיוצרת אובייקט אחד של המחלקה ומחזירה אותו. לא ניכנס ללמה זה טוב. [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p>החלטתי היום לבצע מבחן, רציתי לבדוק מה מהבאים הכי מהיר ובאיזה הפרש.<br />
המועמדים הם:</p>
<ul>
<li>פונקציה פשוטה וסטנדרטית.</li>
<li>מחלקה רגילה, יצירת אובייקט וקריאות לפעולה בו.</li>
<li>מחלקה עם פעולה סטטית וקריאות ישירות אליה.</li>
<li>שימוש בDesign Pattern שנקרא Singleton שבו יוצרים מחלקה רגילה ופעולה סטטית אחת שיוצרת אובייקט אחד של המחלקה ומחזירה אותו. לא ניכנס ללמה זה טוב.</li>
</ul>
<p>כתבתי את כל אחד מהקודים, הפונקציה בסך הכל מחשבת 1+1.<br />
כל אופצייה נקראה 1,000,000 פעמים (בלולאת for) וכך קיבלתי תוצאות זמן די מדוייקות. התוצאות היו עקביות מאוד בין הרצה להרצה.<br />
התוצאות מעט הפתיעו אותי!</p>
<p><strong>פונקציה פשוטה: </strong>0.38151597976685 שניות. (נסמן ב100%)<br />
<strong>מחלקה רגילה: </strong>0.97387981414795 שניות. <strong>255% מפונקציה פשוטה!</strong><br />
<strong>פעולה סטטית: </strong>1.8012449741364 שניות. <strong>472% מפונקציה פשוטה!</strong><br />
<strong>Singleton וקריאה רגילה: </strong>3.3634400367737 שניות. <strong>881% מפונקציה פשוטה!</strong></p>
<p>התוצאה של פונקציה פשוטה ממש לא הפתיע אותי ביחס לשאר, אך האיטיות של פעולה סטטית הפתיעה אותי מאוד. האמנתי שהתוצאות של מחלקה רגילה ושל פעולה סטטית יהיו הפוכות.</p>
<p>המבחנים בוצעו על wamp עם php 5.3.0, במחשב עם מעבד אינטל פנטיום 4 במהירות 3.2GHz ו1GB זכרון.<br />
את <a href="http://www.sup.co.il/44957623">הקבצים שאיתם ביצעתי את ההשוואה</a> תוכלו להוריד.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%91%d7%99%d7%a6%d7%95%d7%a2%d7%99%d7%9d-%d7%a4%d7%95%d7%a0%d7%a7%d7%a6%d7%99%d7%95%d7%aa-oop-%d7%95%d7%a1%d7%98%d7%98%d7%99%d7%95%d7%aa/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#8235;הפשטה ופולימורפיזם – Abstraction and Polymorphism&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%a4%d7%a9%d7%98%d7%94-%d7%95%d7%a4%d7%95%d7%9c%d7%99%d7%9e%d7%95%d7%a8%d7%a4%d7%99%d7%96%d7%9d-%e2%80%93-abstraction-and-polymorphism/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%a4%d7%a9%d7%98%d7%94-%d7%95%d7%a4%d7%95%d7%9c%d7%99%d7%9e%d7%95%d7%a8%d7%a4%d7%99%d7%96%d7%9d-%e2%80%93-abstraction-and-polymorphism/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 13:52:13 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[פרוייקטים]]></category>
		<category><![CDATA[שפות תכנות]]></category>
		<category><![CDATA[תכנות מונחה עצמים]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[לימוד]]></category>
		<category><![CDATA[תכנות אתרים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=425</guid>
		<description><![CDATA[&#8235;שים לב: הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, לאינדקס הפוסטים של תכנות מונחה עצמים. כל הכבוד! הגעתם לנושאים המרתקים ביותר בתכנות מונחה עצמים. הסיבה האמיתית שכל העסק הזה משתלם, הפשטה ופולימורפיזם. לפני שנכנס בדיוק למה זה כל אחד מהמושגים ננסה לראות אותם בעולם המוכר לנו. דיברנו כבר על כך שיכול להיות לנו לדוגמה [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><span style="text-decoration: underline;">שים לב:</span> הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, ל<a href="http://www.xn----0hcb6acyq1epc.com/%D7%A4%D7%A8%D7%95%D7%99%D7%99%D7%A7%D7%98-%D7%AA%D7%9B%D7%A0%D7%95%D7%AA-%D7%9E%D7%95%D7%A0%D7%97%D7%94-%D7%A2%D7%A6%D7%9E%D7%99%D7%9D-%D7%90%D7%99%D7%A0%D7%93%D7%A7%D7%A1/">אינדקס הפוסטים של תכנות מונחה עצמים</a>.</p>
<p>כל הכבוד! הגעתם לנושאים המרתקים ביותר בתכנות מונחה עצמים. הסיבה האמיתית שכל העסק הזה משתלם, הפשטה ופולימורפיזם.</p>
<p>לפני שנכנס בדיוק למה זה כל אחד מהמושגים ננסה לראות אותם בעולם המוכר לנו.<br />
דיברנו כבר על כך שיכול להיות לנו לדוגמה דוב קוטב, שכמובן יורש מדוב, שיורש בתורו מטורפים; אולם כשנחשוב על זה אין בטבע אובייקט שהוא טורף, נמצא רק כאלו ממחלקה שיורשת ממנה. התכונה הזו נקראת אבסטרקציה (&quot;הפשטה&quot;).<br />
אבל זה לא נגמר פה, כל טורף יכול לבצע פעולת טריפה, אבל כל אחד עושה את זה בדרך אחרת, אך כולם ללא יוצא מן הכלל יכולים לטרוף. תכונה זו נקראת פולימורפיזם (&quot;רב צורתיות&quot;) &#8211; תוצאה ישירה של אבסטרקציה.</p>
<p><strong>בתכנות ניתן להגדיר פעולות כאבסטרקטיות ובכך לא לכתוב את המימוש שלהן אלא רק את הגדרת הכימוס שלהן ואת הפרמטרים שלהן.</strong><br />
בכדי להצהיר על פעולה או על מחלקה כאבסטרקטית יש לכתוב את המילה abstract לפני ההוראה (לדוגמה: abstract class Bear).<br />
כל <strong>מחלקה שיש בה פעולה אבסטרקטית אחת ומעלה חייבת להיות מוגדרת כאבסטרקטית</strong> בעצמה.<br />
<strong>ניתן להגדיר מחלקות כאבסטרקטיות גם אם אין בהן פעולות אבסטרקטיות</strong>.<br />
<strong>לא ניתן ליצור מופעים של מחלקה אבסטרקטית</strong>, אך כן ניתן לרשת ממנה.<br />
<strong>מחלקה היורשת ממחלקה אבסטרקטית חייבת לממש את כל הפעולות האבסטרקטיות</strong> שהיא יורשת.</p>
<p>פולימורפיזם הוא מה שמאפשר לנו להתייחס לכמה אובייקטים &#8211; שמחלקותיהם יורשות מאותה מחלקה &#8211; בצורה דומה. כך שניתן להריץ את הפעולה eat עם אותם פרמטרים על אובייקטים של המחלקות Bear וLion, היורשות מהמחלקה Carnivore.<br />
בשפות בהן יש להצהיר על משתנים ולציין את סוגם (כמו C, ASP.Net, Java וכו') יש לכך יתרון נוסף, ניתן להצהיר על משתנה מסוג Carnivore ולהציב בו אובייקט של המחלקה Lion! פשוט כי היא יורשת ממנו!</p>
<p>אתן דוגמה לישום פשוט שכולל מחלקות פשוטות שמנהלות databaseים מסוג MySQL וSQLite. נצא מתוך נקודת הנחה שיש לנו database עם טבלה pages שמכילה עמודת name וcontent.<br />
היישום ישלוף דף לפי מה שמוגדר בget ויציג אותו, אם לא ימצא דף יציג הודעת שגיאה.</p>
<p>נכתוב את המחלקות:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> abstract_db <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$con</span><span style="color: #339933;">;</span>
&nbsp;
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$con_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> num_rows<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> escape_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> mysql_db <span style="color: #000000; font-weight: bold;">extends</span> abstract_db <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$con_data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$con_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$con_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'database'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> num_rows<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> escape_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> sqlite_db <span style="color: #000000; font-weight: bold;">extends</span> abstract_db <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$con_data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sqlite_open</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">sqlite_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> num_rows<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">sqlite_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> escape_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">sqlite_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">sqlite_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">sqlite_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</div>
<p>ראשית הצהרנו על מחלקה אבסטרקטית ולה מאפיין אחד שמחזיק את החיבור למסד הנתונים ו6 פעולות שכולן אבסטרקטיות.<br />
שנית הצהרנו על מחלקה mysql_db המרחיבה את abstract_db ומימשנו בה את כל הפעולות. לבסוף כתבנו מחלקה sqlite_db דומה מאוד למחלקת mysql_db.</p>
<p>נכתוב את גוף היישום, (כך שישתמש בdatabase מסוג mysql):</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysql_db<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'host'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'example'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT name, content FROM pages WHERE name = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid page!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>השורה הראשונה יוצרת חיבור למסד נתונים מסוג mysql השנייה והשלישית מריצות שאילתה מאובטחת למציאת הדף. הרביעית, החמישית והשישית מונעות גישה לדפים שאינם קיימם. לבסוף שלפנו את תוצאות השאילתה והדפסנו את שם הדף ככותרת ואת התוכן בתוך פסקה.</p>
<p>בכדי לעבור לשימוש בsqlite בדרך כלל (אם לא היינו משתמשים בשכבת אבסטרקציה בדומה לזו) היינו צריכים לעבור שורה שורה ולהחליף את כל הפקודות mysql_* לפקודות הsqlite המתאימות להן; אך ביישום הזה כל שעלינו לעשות זה להחליף את השורה הראשונה לזו:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sqlite_db<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'example.db'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p>זהו זה, זה הכל!</p>
<p>כפי שאתם רואים מעבר לתכנות מונחה עצמים יוצר קוד מסודר יותר ונקי יותר המאפשר דינאמיות גבוהה יותר בזכות תכונות הכימוס, הורשה, אבסטרקציה ופולימורפיזם שאינן קיימות בתכנות פרוצדורלי.<br />
כאן אמור להסתיים הפרוייקט אך למעשה זהו סופו של העיקר, אך לא הכל. מספר מאמרים נוספים יתווספו וימשיכו מכאן והלאה.</p>
<p>בהצלחה!</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%a4%d7%a9%d7%98%d7%94-%d7%95%d7%a4%d7%95%d7%9c%d7%99%d7%9e%d7%95%d7%a8%d7%a4%d7%99%d7%96%d7%9d-%e2%80%93-abstraction-and-polymorphism/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8235;הורשה – Inheritance&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%95%d7%a8%d7%a9%d7%94-%e2%80%93-inheritance/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%95%d7%a8%d7%a9%d7%94-%e2%80%93-inheritance/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 20:55:57 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[פרוייקטים]]></category>
		<category><![CDATA[שפות תכנות]]></category>
		<category><![CDATA[תכנות מונחה עצמים]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[לימוד]]></category>
		<category><![CDATA[תכנות אתרים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=417</guid>
		<description><![CDATA[&#8235;שים לב: הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, לאינדקס הפוסטים של תכנות מונחה עצמים. עמלנו קשות וכעת אנו יודעים כיצד ליצור מחלקות שלמות עבור אובייקטים שונים מה שמוביל אותנו לנושאים הבאים. אך עד כה השימוש במחלקות הביא לנו תועלת די מעוטה ביחס לנושאים הקרובים שבהם נדון. אם נחשוב על זה העולם לא מחולק [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><span style="text-decoration: underline;">שים לב:</span> הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, ל<a href="http://www.xn----0hcb6acyq1epc.com/%D7%A4%D7%A8%D7%95%D7%99%D7%99%D7%A7%D7%98-%D7%AA%D7%9B%D7%A0%D7%95%D7%AA-%D7%9E%D7%95%D7%A0%D7%97%D7%94-%D7%A2%D7%A6%D7%9E%D7%99%D7%9D-%D7%90%D7%99%D7%A0%D7%93%D7%A7%D7%A1/">אינדקס הפוסטים של תכנות מונחה עצמים</a>.</p>
<p>עמלנו קשות וכעת אנו יודעים כיצד ליצור מחלקות שלמות עבור אובייקטים שונים מה שמוביל אותנו לנושאים הבאים. אך עד כה השימוש במחלקות הביא לנו תועלת די מעוטה ביחס לנושאים הקרובים שבהם נדון.</p>
<p>אם נחשוב על זה העולם לא מחולק רק לעצמים שונים ממחלקות שונות כמו מסך המחשב שלי, מסך המחשב שלך, שולחן הסלון שלי, העיפרון שלי וכדומה, אלא לפעמים יש ביניהם קשר של הורשה. אפשר לראות איזשהי הכלה מסויימת של אחד בתוך השני.<br />
לדוגמה, למסך מחשב LCD ולמסך טלויזיה LCD יש הרבה מאוד במשותף, שניהם מסכי LCD ומכאן נובעות תכונות רבות עליהם. חוץ מזה יש גם מסך טלויזיה Plasma וCRT וגם לכל אלו משהו במשותף, כולם טלויזיות. לא רק זה, אלא גם שכל אלו הם מסכים!<br />
דוגמה מאוד יפה לזה זה החלוקה של האדם של הטבע. כל בעל חיים מקוטלג לסוג, למשפחה, למחלקה, מערכה וכדומה. לדוגמה דוב קוטב הוא מסוג דוב, ממשפחת הדוביים, מסדרת הטורפים, ממחלקת היונקים, ממערכת המיתרניים וממלכת בעלי החיים. תוכלו לראות שלכל שכבה כזאת יש המון במשותף ולכן אוגדו לקבוצה.</p>
<p>בקיצור לאובייקטים רבים יש תכונות רבות במשותף, לכן פעמים רבות נעדיף לכתוב מימוש כלשהו ואותו נרחיב לאחר מכן. ניתן לבצע זאת בתכנות מונחה עצמים.<br />
הערה: בשפות רבות שבהן יש להצהיר על סוג המשתנה (Strong Typing) יש לכך יתרונות נוספים, משתנה מסוג מסך יכול להכיל אובייקט מסוג מסך מחשב, אך לא להיפך. אתם וודאי מכירים את הדוגמה הקלאסית של ריבוע הוא מרובע אך מרובע אינו בהכרח ריבוע, זהו בדיוק המקרה.</p>
<p>נכתוב מחלקה (שלא ממש יעילה אך ממחישה את העקרונות בקלות) בעל חיים טורף ומחלקה שמרחיבה אותה שמייצגת דוב ומחלקה שמרחיבה אותה שהיא דוב קוטב.<br />
שימו לב, במאמר הבא בנושא הפשטה נלמד כיצד יכולנו לכתוב זאת אף יותר טוב, אך אין זה משנה כרגע.</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Carnivore <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$legs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$speed_kmh</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$eyes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Leather'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_length_cm</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$can_eat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> eatable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$eatable_thing</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$eatable_thing</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_eat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_eat</span><span style="color: #339933;">,</span> <span style="color: #000088;">$eatable_thing</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_eat</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$eatable_thing</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Bear <span style="color: #000000; font-weight: bold;">extends</span> Carnivore <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Fur'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_length_cm</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$can_eat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fish'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rabbits'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$roar_volume_dba</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">75</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> roar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Rooaarrr!!&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_fish<span style="color: #009900;">&#40;</span><span style="color: #000088;">$amount</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Go to the river and get a fish</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$for_hrs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Go to the cave and sleep</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Polar_Bear <span style="color: #000000; font-weight: bold;">extends</span> Bear <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_color</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'White'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_length_cm</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_fish<span style="color: #009900;">&#40;</span><span style="color: #000088;">$amount</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Break ice first and get fish from there</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$for_hrs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Go to a lonley place and sleep</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$carnivore</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Carnivore<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$carnivore</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cover_type</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs Leather</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$carnivore</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Carnivore'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs yes</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$carnivore</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Bear'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs no</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$carnivore</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Polar_Bear'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs no</span>
&nbsp;
<span style="color: #000088;">$bear</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$bear</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cover_type</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs Fur</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bear</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Carnivore'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs yes</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bear</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Bear'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs yes</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bear</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Polar_Bear'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs no</span>
&nbsp;
<span style="color: #000088;">$polar_bear</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Polar_Bear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$bear</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cover_type</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs Fur</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$polar_bear</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Carnivore'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs yes</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$polar_bear</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Bear'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs yes</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$polar_bear</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Polar_Bear'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Outputs yes</span></pre></td></tr></table></div>

</div>
<p>כרגיל נעבור על הקוד.<br />
ראשית הצהרנו על מחלקה פשוטה שמייצגת טורף.<br />
שנית הצהרנו על מחלקה שמייצגת דוב ומרחיבה את מחלקת הטורף (Bear <em>extends</em> Carnivore). במבט קליל נגלה שכתבנו כמה מאפיינים שכבר הוזכרו במחלקת הטורף. מה שיקרה בפועל זה שמחלקת הטורף תשוכפל ובכל מקום בו כתבנו משהו בשם חדש הוא יוחלף. הצהרנו כאן גם על מאפיין חדש לחלוטין וכמה פעולות חדשות שפשוט יתווספו.<br />
שלישית הצהרנו על מחלקה שמייצגת דוב קוטב ומרחיבה את מחלקת הדוב. כאן הצהרנו על מחדש על מספר מאפיינים ופעולות ויקרה דבר זהה למה שקרה בנושא מחלקת הדוב והטורף.<br />
לבסוף יצרתי אובייקט של כל אחת מהמחלקות, לשם הדוגמה הדפסתי את סוג הכיסוי של כל אחד מהאובייקטים והפלטים היו: Leather, Fur וFur. עוד דוגמה הייתה הפונקצייה is_a, שבודקת האם אובייקט הוא מופע של מחלקה מסויימת או בן של אותה מחלקה. הפלטים די ברורים.</p>
<p>בכדי להסביר מעט, נניח והינו רוצים מחלקה של דוב קוטב ללא ירושה משאר המחלקות, התוצאה הייתה שכזו:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Polar_Bear <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$legs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$speed_kmh</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$eyes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Fur'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_length_cm</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$can_eat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fish'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rabbits'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$roar_volume_dba</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">75</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cover_color</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'White'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> eatable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$eatable_thing</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$eatable_thing</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_eat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_eat</span><span style="color: #339933;">,</span> <span style="color: #000088;">$eatable_thing</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_eat</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$eatable_thing</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> roar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Rooaarrr!!&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_fish<span style="color: #009900;">&#40;</span><span style="color: #000088;">$amount</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Break ice first and get fish from there</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$for_hrs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Go to a lonley place and sleep</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</div>
<h3>למה כל זה טוב?</h3>
<p>למעשה כאן מתחילה היעילות האמיתית של תכנות מונחה עצמים על פני תכנות פרוצדורלי. שימוש פופולארי לזה זה מסדי נתונים, אך במאמר הבא נבין יותר מדוע.<br />
אפרט מספר יתרונות:</p>
<ul>
<li>חיסכון עצום בקטעי קוד שחוזרים על עצמים.</li>
<li>אפשרות לקחת שני אובייקטים שונים השייכים לאותו הורה ולהשתמש בהם בצורה זהה לחלוטין (כמובן שלכל אחד יהיו תכונות נוספות). נושא זה נקרא <em>פולימורפיזם</em> ועליו נדון במאמר הבא.</li>
<li>אפשרות להשתמש במחלקות רבות ושונות ללא צורך בהתעסקות עם המימוש הפנימי שלהן.</li>
</ul>
<p>בהצלחה!</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%94%d7%95%d7%a8%d7%a9%d7%94-%e2%80%93-inheritance/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#8235;כימוס – Encapsulation&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%9b%d7%99%d7%9e%d7%95%d7%a1-%e2%80%93-encapsulation/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%9b%d7%99%d7%9e%d7%95%d7%a1-%e2%80%93-encapsulation/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 19:44:40 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[פרוייקטים]]></category>
		<category><![CDATA[תכנות מונחה עצמים]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[לימוד]]></category>
		<category><![CDATA[מונחים]]></category>
		<category><![CDATA[תכנות אתרים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=405</guid>
		<description><![CDATA[&#8235;שים לב: הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, לאינדקס הפוסטים של תכנות מונחה עצמים. כימוס הוא נושא די פשוט מבחינה תחבירית ומבחינת שימוש אך יש לו שימושים רבים ולעתים מעט מפולפלים. הוא מהווה אבן יסוד חשובה ביותר בתכנות מונחה עצמים והגנה על מידע בתוך אובייקט. המונח כימוס מתייחס ליכולת של מחלקות ליצור מאפיינים [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><span style="text-decoration: underline;">שים לב:</span> הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, ל<a href="http://www.xn----0hcb6acyq1epc.com/%D7%A4%D7%A8%D7%95%D7%99%D7%99%D7%A7%D7%98-%D7%AA%D7%9B%D7%A0%D7%95%D7%AA-%D7%9E%D7%95%D7%A0%D7%97%D7%94-%D7%A2%D7%A6%D7%9E%D7%99%D7%9D-%D7%90%D7%99%D7%A0%D7%93%D7%A7%D7%A1/">אינדקס הפוסטים של תכנות מונחה עצמים</a>.</p>
<p>כימוס הוא נושא די פשוט מבחינה תחבירית ומבחינת שימוש אך יש לו שימושים רבים ולעתים מעט מפולפלים.<br />
הוא מהווה אבן יסוד חשובה ביותר בתכנות מונחה עצמים והגנה על מידע בתוך אובייקט.</p>
<p>המונח כימוס מתייחס ליכולת של מחלקות ליצור מאפיינים ופעולות שמוסתרות ומסתירות מימוש של חלקים מסויימים בעזרת הוראות בודדות.<br />
במילים אחרות, אפשר ליצור צד ציבורי (<strong>public</strong>) וצד פרטי (<strong>private</strong>) כך שבתוך האובייקט והמחלקה ניתן לגשת לשניהם אך מבחוץ רק לצד הציבורי.</p>
<p>ישנן 3 רמות שונות של גישות:</p>
<ul>
<li><strong>public</strong> &#8211; מצב <em>ברירת מחדל</em>, ניתן לגשת לפעולה או למאפיין מכל מקום.</li>
<li><strong>private</strong> &#8211; ניתן לגשת לפעולה או למאפיין רק מתוך פעולות פנימיות אחרות של המחלקה.</li>
<li><strong>protected</strong> &#8211; דומה לprivate, אך מאפשר גישה למחלקות היורשות (יוסבר בפוסט הבא בסדרה) מהמחלקה הזו.</li>
</ul>
<p>עד עכשיו בכדי ליצור מאפיין למחלקה רשמנו את המילה var ואחריה את שם המאפיין ופעולות רשמנו כמו שאנו רושמים פונקציות מחוץ למחלקה.<br />
מעכשיו והלאה לפני מאפיינים נרשום public/private/protected במקום המילה var ולפני כל פעולה נרשום public/private/protected.</p>
<h3>מספר יתרונות</h3>
<p>לעתים נרצה שמידע כלשהו שקיים באובייקט ישמר ויועבר אך אנו לא רוצים שיהיה אפשר לגשת אליו מבחוץ מסיבות כמו אבטחת מידע, שמירה על תקינות נתונים, החזקת נתונים זמניים וכדומה.<br />
בנוסף יהיו מצבים שפעולות מסויומות יהיו קוד פנימי כלשהו, שאנו לא רוצים שיהיה זמין אך הוא כן דרוש למספר פעולות במחלקה, לדוגמה קטעים לוגיים המשותפים למספר פעולות שאנו רוצים לפרק לחלקים ובכך לקצרן, פעולות שנועדו לרוץ בזמנים מסויימים ועוד.</p>
<h3>דוגמה</h3>
<p>ניתן דוגמה לשימוש כמעט אמיתי של מחלקות ושל כימוס, נכתוב מחלקה של משתמש קונה באתר קניות ושל מוצר באתר קניות. מטרתנו היא לכתוב מחלקה שבהינתן username וpassword של משתמש היא מאפשרת לנו למצוא מידע עליו, לבצע פעולת רכישה ולעדכן את שמו. ובנוסף מחלקה שמייצגת מוצר כלשהו, שבהינתן id היא מוצאת את מחיר ושם המוצר, אובייקטים שלה מועברים לפעולת הרכישה.<br />
נניח שקיים כבר חיבור למסד נתונים mysql וקיימת טבלה Customers שמכילה עמודות של id, username, password, fullname, credit_card וטבלת Products שמכילה עמודות של id, name, price.</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$price</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name, price FROM Products WHERE id = '<span style="color: #006699; font-weight: bold;">{$this-&gt;id}</span>' LIMIT 0,1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">price</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id, username, password, full_name, credit_card FROM Customers WHERE username = '<span style="color: #006699; font-weight: bold;">{$username}</span>' AND password = '<span style="color: #006699; font-weight: bold;">{$password}</span>' LIMIT 0,1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> credit_card_valid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Make sure there are enough digits and all this stuff</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$is_vaild</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buy<span style="color: #009900;">&#40;</span>Product <span style="color: #000088;">$prod</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// First make sure the credit card is valid</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">credit_card_valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Call an imiginary function which takes money from a credit card</span>
			make_a_payment_using_credit_card<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;credit_card&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$prod</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">price</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// Submit an email to the site administrator that a new product has been bought</span>
			let_admin_know_by_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$prod</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Thank you &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">full_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;, for buying at our shop. The admin will contact you soon.&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;full_name&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$new_name</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$new_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE Customers SET full_name = '<span style="color: #006699; font-weight: bold;">{$new_name}</span>' WHERE id = '<span style="color: #006699; font-weight: bold;">{$this-&gt;data['id']}</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> full_name<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;full_name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Lets look for the user yossi22</span>
<span style="color: #000088;">$yossi</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yossi22&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;123abc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Excpected to print something like &quot;Yossi Avraham&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$yossi</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">full_name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// We are now using the product class, and look for the product with the id 12345</span>
<span style="color: #000088;">$new_television</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Product<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">12345</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Lets make Yossi buy a new television</span>
<span style="color: #666666; font-style: italic;">// If he gets it let him know</span>
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$yossi</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_television</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Will out put &quot;Thank you Yossi Avraham, for buying at our shop. The admin will contact you soon.&quot;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Maybe Yontan want a new TV two?</span>
<span style="color: #000088;">$yonatan</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yonatan1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;976431&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Wait! he changed his name to Ohad Regev</span>
<span style="color: #000088;">$yonatan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update_name</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ohad Regev&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$yonatan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_television</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Will out put &quot;Thank you Ohad Regev, for buying at our shop. The admin will contact you soon.&quot;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</div>
<p>בואו ננסה להבין קטע אחרי קטע:<br />
ראשית כתבנו מחלקת Product המכילה 3 משתנים ציבוריים id, name וprice &#8211; מספר סידורי, שם ומחיר ולה פעולת Constructor ציבורית שמקבלת id ומוצאת את המוצר שפרטיו תואמים לid זה.<br />
שנית הצהרנו על מחלקה בשם Customer, היא מכילה משתנה פרטי בשם data, פעולת Constructor ציבורית המקבל שם משתמש וסיסמה, פעולת רכישה ציבורית המקבלת מוצר (שימו לב לארגומנט הראשון, ניתן להגדיר את סוג המחלקה שאנו מצפים לקבל) ומשתמשת בפעולה פרטית המוודאת תקינות של כרטיס אשראי, פעולה ציבורית לעדכון השם ופעולה ציבורית להחזרת השם. לא אכנס לתוכן כל פעולה כיוון שהן די פשוטות וזניחות.<br />
לאחר מכן יצרנו אובייקט בשם yossi שהוא מופע של המחלקה Customer והעברנו שם משתמש וסיסמה, הדפסנו את השם המלא של יוסי. כעת ניצור אובייקט new_television שהוא מופע של המחלקה Product עם הid שמספרו 12345.<br />
לאחר מכן נגיד ליוסי לקנות טלוויזיה חדשה, ואם הוא הצליח לקנות אז נדפיס הודעה שמכילה את השם שלו.<br />
מאוחר יותר ביצענו תהליך דומה למשתמש אחר בשם יונתן, ששינה את שמו לאוהד. לאחר שקנה טלוויזיה חדשה תודפס לו הודעה המכילה את שמו החדש.</p>
<h3>גישה למידע פרטי</h3>
<p>כעת נבדוק מה קורה כאשר אנו מנסים לגשת לקוד שמוגדר כפרטי &#8211; private.<br />
נוסיף את הקוד הבא לאחר ההצהרה על המשתנה yossi:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$yossi</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p>כמו שוודאי קראתם המאפיין data הוא פרטי ולכן לא ניתן לגשת אליו מחוץ למחלקה עצמה. נסיון לגשת אליו תגרור פלט הדומה לפלט הבא:</p>
<div style="text-align: left; direction: ltr;">Fatal error: Cannot access private property Customer::$data in &#8230;\index.php  on line 58</div>
<p>במילים פשוטות, הסקיפט יעצר באותה הנקודה כיוון שלא ניתן לגשת למאפיינים ופעולות פרטיות.<br />
נסיון לגשת לפעולה הפרטית credit_card_valid יוביל לתוצאה דומה, עבור הקוד:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$yossi</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">credit_card_valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p>נקבל פלט שכזה:</p>
<div style="text-align: left; direction: ltr;">Fatal error: Call to private method Customer::credit_card_valid() from context &quot; in &#8230;\index.php  on line 58</div>
<h3>סיכום</h3>
<p>במחלקה הגנו על מספר נתונים: על מספר כרטיס האשראי, על שם המשתמש ועל הסיסמה. הם אינם נגישים מחוץ לאובייקט אך כן בתוכו.<br />
בנוסף יצרנו פעולה שמחזירה את המידע מהמאפיין הפרטי, לפעולות שכאלו נהוג לקרוא <em>getters</em>, לפעולות שמקבלות מידע ומכניסות אותו למשתנה (בדומה לפעולה update_name) נהוג לקרוא <em>setters</em>.<br />
השתמשנו בפעולה פרטית בשם credit_card_valid בכדי לוודא לפני רכישה האם מספר הכרטיס קיים ואמיתי.</p>
<p>אני מקווה שהדוגמה הבהירה לכם באילו מצבים יש להשתמש במאפיינים ופעולות ציבוריים ובאילו מקרים יש להשתמש בפרטיים, כמובן שכל זאת נתון לשיקול דעתכם.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%9b%d7%99%d7%9e%d7%95%d7%a1-%e2%80%93-encapsulation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#8235;יישום של תכנות מונחה עצמים&#8236;</title>		<link>http://www.xn----0hcb6acyq1epc.com/%d7%99%d7%99%d7%a9%d7%95%d7%9d-%d7%a9%d7%9c-%d7%aa%d7%9b%d7%a0%d7%95%d7%aa-%d7%9e%d7%95%d7%a0%d7%97%d7%94-%d7%a2%d7%a6%d7%9e%d7%99%d7%9d/</link>
		<comments>http://www.xn----0hcb6acyq1epc.com/%d7%99%d7%99%d7%a9%d7%95%d7%9d-%d7%a9%d7%9c-%d7%aa%d7%9b%d7%a0%d7%95%d7%aa-%d7%9e%d7%95%d7%a0%d7%97%d7%94-%d7%a2%d7%a6%d7%9e%d7%99%d7%9d/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 12:46:03 +0000</pubDate>
		<dc:creator>&#8235;Shay&#8236;</dc:creator>				<category><![CDATA[פרוייקטים]]></category>
		<category><![CDATA[תכנות מונחה עצמים]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[לימוד]]></category>
		<category><![CDATA[תכנות אתרים]]></category>

		<guid isPermaLink="false">http://www.xn----0hcb6acyq1epc.com/?p=382</guid>
		<description><![CDATA[&#8235;שים לב: הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, לאינדקס הפוסטים של תכנות מונחה עצמים. בואו וניקח את הרעיונות והשמות שדיברנו עליהם ונהפוך אותם לקוד של ממש. את הקוד אכתוב בPHP כיוון שזה בלוג בנושא בנית אתרים אולם קוד דומה ישמש לשפות רבות ומה שישתנה זה כמעט אך ורק התחביר. נכתוב מחלקה שמייצגת כלב [...]&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><span style="text-decoration: underline;">שים לב:</span> הפוסט הזה מסתמך על ידע מפוסטים קודמים בסדרה, ל<a href="http://www.xn----0hcb6acyq1epc.com/%D7%A4%D7%A8%D7%95%D7%99%D7%99%D7%A7%D7%98-%D7%AA%D7%9B%D7%A0%D7%95%D7%AA-%D7%9E%D7%95%D7%A0%D7%97%D7%94-%D7%A2%D7%A6%D7%9E%D7%99%D7%9D-%D7%90%D7%99%D7%A0%D7%93%D7%A7%D7%A1/">אינדקס הפוסטים של תכנות מונחה עצמים</a>.</p>
<p>בואו וניקח את הרעיונות והשמות שדיברנו עליהם ונהפוך אותם לקוד של ממש.<br />
את הקוד אכתוב בPHP כיוון שזה בלוג בנושא בנית אתרים אולם קוד דומה ישמש לשפות רבות ומה שישתנה זה כמעט אך ורק התחביר.</p>
<p>נכתוב <em>מחלקה</em> שמייצגת כלב ולאחר מכן ניצור את לאסי ה<em>אובייקט</em> שהוא <em>מופע</em> של כלב.</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This is the class Dog</span>
<span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// This is the property speed which represents dogs speed</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$speed</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// This is the method talk</span>
	<span style="color: #000000; font-weight: bold;">function</span> talk<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;woof woof&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// This is the method walk, which receives 1 argument and probabbly uses the speed property</span>
	<span style="color: #000000; font-weight: bold;">function</span> walk<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// This is just an example, implementation of walk method should be here</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Lets create an object of Dog named Lassie</span>
<span style="color: #000088;">$lassie</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Lets make Lassie talk</span>
<span style="color: #000088;">$lassie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">talk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Lets change the speed of Lassie</span>
<span style="color: #000088;">$lassie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">speed</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Lets make Lassie walk some where</span>
<span style="color: #000088;">$lassie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">walk</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;some where&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>אסביר את הקוד:<br />
<strong style="direction: ltr;">class Dog</strong> &#8211; המילה השמורה class מציינת פתיחת מחלקה.<br />
<strong style="direction: ltr;">var $speed</strong> &#8211; המילה var (בהמשך נלמד על public, protected וprivate) מגדירה מאפיין למחלקה.<br />
<strong>הפונקצייה talk וwalk</strong> &#8211; פעולות של המחלקה, walk מקבל ארגומנט אחד.<br />
<strong style="direction: ltr;">$lassie = new Dog();</strong> &#8211; המילה השמורה new יוצרת מופע של המחלקה שרשומה אחריה. בקרוב נבין מה משמעות הסוגריים.<br />
<strong style="direction: ltr;">$lassie-&gt;talk();</strong> &#8211; הסימן &quot;-&gt;&quot; משמש לשם פנייה לאיברים (מאפיינים ופעולות) של המחלקה, קריאה לפעולה talk.<br />
<strong style="direction: ltr;">$lassie-&gt;speed = 10;</strong> &#8211; שינוי של המאפיין speed ל10.<br />
<strong style="direction: ltr;">$lassie-&gt;walk(&quot;some where&quot;);</strong> &#8211; קריאה לפעולה walk עם ארגומנט 1.<br />
הפלט של הקוד הזה יהיה כמובן &quot;woof woof&quot;.</p>
<p>בואו ננסה להבין יחד למה זה טוב. נניח שאת המחלקה Dog כתב מתכנת א' והוא השתמש בה בכדי ליצור את לאסי, אבל חודשיים לאחר מכן מתכנת ב' רצה ליצור כלב חדש &#8211; פלאפי. ללא מחלקה הוא היה צריך לקחת את הקוד של לאסי ולשכפל אותו בכל נקודה ולהבין את הלוגיקה שמאחורי כל פקודה ופקודה (במקרים שכל פעולה מכילה עשרות, מאות ואלפי שורות זה יותר משמעותי).<br />
עם מחלקות הוא יכול לכתוב קוד שכזה:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$flaffi</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$lassie</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">speed</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$flaffi</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">walk</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;here&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$flaffi</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">talk</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<h3>פעולת Constructor (&quot;בנאי&quot;) והמשתנה $this</h3>
<p>בכל מחלקה ניתן להשתמש במשתנה הפנימי $this. משתנה זה הוא למעשה מפנה למשתנה של האובייקט עצמו כך שאם אוסיף את הקוד <span style="direction: ltr;">$this-&gt;speed = 10;</span> לפעולה talk, ברגע שהיא תורץ המאפיין speed של האובייקט עצמו (לדוגמה $lassie) ישתנה ל10.</p>
<p>לעתים נרצה להפעיל פעולות מסויימות, לקבל משתנים לתוך האובייקט וכדומה עוד בזמן יצירתו, לשם כך קיימת פעולת Constructor.<br />
הפעולה היא פעולה רגילה לחלוטין אך היא נקראת באופן אוטומטי ברגע היצירה של האובייקט, כמובן שאין חובה ליצור אחת כזו. שם הפעולה צריך להיות זהה לשם המחלקה (במרבית שפות התכנות וגם בPHP), אך מPHP5 ומעלה מומלץ לקרוא לה __construct (כולל שני הקווים התחתונים).<br />
נחזור לדוגמה הקודמת, נניח שאנו רוצים לקבוע את מהירות ההליכה של הכלב בזמן שאנו יוצרים אותו, נוסיף למחלקה את הקוד הבא:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">function</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #000088;">$speed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">speed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$speed</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</div>
<p>לאחר מכן, נצטרך לשנות את היצירה של לאסי ופלאפי לשורות הבאות (ונוכל למחוק את השורות שמשנות את המהירות באופן חיצוני):</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$lassie</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$flaffi</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<h3>תצורת הזיכרון של משתני אובייקטים</h3>
<p>אובייקטים נשמרים בתצורה הנקראת Reference Type בעוד משתנים &quot;פרמיטיבים&quot; כמו מספרים נשמרים בתצורה הנקראת Value Type.<br />
ההבדל בין השתיים הוא שבמשתנים מסוג Value Type המשתנה מקבל את הערך עצמו, בעוד באובייקטים המשתנה מצביע למיקום שבו נמצא הערך!</p>
<p>קוד להמחשה:</p>
<div style="text-align: left; direction: ltr;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Will print out 5</span>
&nbsp;
<span style="color: #000088;">$dog1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Init the speed to 5</span>
<span style="color: #000088;">$dog2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dog1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dog2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">speed</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dog1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">speed</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Will print out 8</span></pre></td></tr></table></div>

</div>
<p>לקריאה נוספת בעברית &#8211; <a href="http://www.startupseeds.com/forums/dev/t67797-value_type_amp_reference_in_php">Value Type &amp; Reference Type in PHP</a> (נכתב על ידי איתי סלע).</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.xn----0hcb6acyq1epc.com/%d7%99%d7%99%d7%a9%d7%95%d7%9d-%d7%a9%d7%9c-%d7%aa%d7%9b%d7%a0%d7%95%d7%aa-%d7%9e%d7%95%d7%a0%d7%97%d7%94-%d7%a2%d7%a6%d7%9e%d7%99%d7%9d/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

