<?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>Abstract Class &#8211; Best DevOps</title>
	<atom:link href="https://www.bestdevops.com/tag/abstract-class/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.bestdevops.com</link>
	<description>Lets Learn, Do it &#38; Share! Thats a Best DevOps!!!</description>
	<lastBuildDate>Wed, 01 Jun 2022 06:34:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>
	<item>
		<title>How to Create a class in PHP</title>
		<link>https://www.bestdevops.com/how-to-create-a-class-in-php/</link>
					<comments>https://www.bestdevops.com/how-to-create-a-class-in-php/#respond</comments>
		
		<dc:creator><![CDATA[anil]]></dc:creator>
		<pubDate>Mon, 02 May 2022 06:52:00 +0000</pubDate>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Abstract Class]]></category>
		<category><![CDATA[Abstract Method]]></category>
		<category><![CDATA[Defining Interface:]]></category>
		<category><![CDATA[Difference between interface and abstract class:]]></category>
		<category><![CDATA[interface]]></category>
		<guid isPermaLink="false">https://www.bestdevops.com/?p=23407</guid>

					<description><![CDATA[Interface: An interface is like a class with nothing but abstract methods. All methods of an interface must be public. [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Interface:</h2>



<p class="wp-block-paragraph">An interface is like a class with nothing but abstract methods. All methods of an interface must be public. It is also possible to declare a constructor in an interface. It’s possible for interface to have constants(can not be overridden by a class/interface that inherits them). interface keyword is used to create an interface in php.</p>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" src="https://www.bestdevops.com/wp-content/uploads/2022/04/image-1.png" alt="" class="wp-image-23410" width="413" height="202" srcset="https://www.bestdevops.com/wp-content/uploads/2022/04/image-1.png 816w, https://www.bestdevops.com/wp-content/uploads/2022/04/image-1-300x147.png 300w, https://www.bestdevops.com/wp-content/uploads/2022/04/image-1-768x376.png 768w" sizes="(max-width: 413px) 100vw, 413px" /></figure>



<ul class="wp-block-list"><li>Interface don’t have instance variables.</li><li>All methods of an interface is abstract.</li><li>All methods of an interface are automatically (by default) public.</li><li>We can not use the private and protected specifiers when declaring member of an interface.</li><li>We can not create object of interface.</li><li>More than one interface can be implemented in a single class.</li><li>A class implements an interface using implements keyword.</li><li>If a class is implementing an interface it has to define all the methods given in that interface.</li><li>If a class does not implement all the methods declared in the interface, the class must be declared abstract.</li><li>The method signature for the method in the class must match the method signature as it appears in the interface.</li><li>Any class can use an interface’s constants from the name of the interface like Test::roll.</li><li>Classes that implement an interface can treat the constants as they were inherited.</li><li>An interface can extend (inherit) an interface.</li><li>One interface can inherit another interface using extends keywords.</li><li>An Interface can not extends classes.</li></ul>



<h3 class="wp-block-heading">Defining Interface:</h3>


<pre class="wp-block-code"><span><code class="hljs language-php">Syntax: - 
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">interface_name</span> 
</span>{
	<span class="hljs-keyword">const</span> properties;
	Method;
} 

Ex: - 
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Father</span>  
</span>{
	<span class="hljs-keyword">const</span> a;
	<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">disp</span><span class="hljs-params">( )</span></span>;
} </code></span></pre>


<h3 class="wp-block-heading">Extending Interface:</h3>



<ul class="wp-block-list"><li>An interface can extend (inherit) an interface.</li><li>One interface can inherit another interface using extends keywords.</li><li>An Interface can not extends classes.</li></ul>



<h3 class="wp-block-heading">One Interface extending one Interface:</h3>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://www.bestdevops.com/wp-content/uploads/2022/04/image-3.png" alt="" class="wp-image-23412" width="393" height="207" srcset="https://www.bestdevops.com/wp-content/uploads/2022/04/image-3.png 789w, https://www.bestdevops.com/wp-content/uploads/2022/04/image-3-300x158.png 300w, https://www.bestdevops.com/wp-content/uploads/2022/04/image-3-768x404.png 768w" sizes="(max-width: 393px) 100vw, 393px" /></figure>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">interface_name1</span>
</span>{
	<span class="hljs-keyword">const</span> properties;
	Methods;
}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">interface_name</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">interface_name1</span>
</span>{
	<span class="hljs-keyword">const</span> properties;
	Method;
}</code></span></pre>


<h3 class="wp-block-heading">One interface can extend more than one interface:</h3>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://www.bestdevops.com/wp-content/uploads/2022/04/image-4.png" alt="" class="wp-image-23413" width="352" height="287" srcset="https://www.bestdevops.com/wp-content/uploads/2022/04/image-4.png 829w, https://www.bestdevops.com/wp-content/uploads/2022/04/image-4-300x244.png 300w, https://www.bestdevops.com/wp-content/uploads/2022/04/image-4-768x625.png 768w" sizes="(max-width: 352px) 100vw, 352px" /></figure>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Father</span>
</span>{

}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Mother</span>
</span>{

}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span>, <span class="hljs-title">Mother</span>
</span>{

}
</code></span></pre>


<h3 class="wp-block-heading">Implementing Class:</h3>



<ul class="wp-block-list"><li>More than one interface can be implemented in a single class.</li><li>A class implements an interface using implements keyword.</li><li>If a class is implementing an interface it has to define all the methods given in that interface.</li><li>If a class does not implement all the methods declared in the interface, the class must be declared abstract.</li><li>The method signature for the method in the class must match the method signature as it appears in the interface.</li><li>Any class can use an interface’s constants from the name of the interface like Test::roll.</li><li>Classes that implement an interface can treat the constants as they were inherited.</li></ul>



<h3 class="wp-block-heading">one interface can be implemented in a single class:</h3>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">interface_name</span>
</span>{
	<span class="hljs-keyword">const</span> properties;
	Methods;
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">class_name</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">interface_name</span>
</span>{
	Properties;
	Methods;
}</code></span></pre>


<h3 class="wp-block-heading">More than one interface can be implemented in a single class:</h3>


<pre class="wp-block-code"><span><code class="hljs language-php">{

}

<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Mother</span>
</span>{

}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Father</span>, <span class="hljs-title">Mother</span>
</span>{

}</code></span></pre>


<h2 class="wp-block-heading">Abstract Class:</h2>



<p class="wp-block-paragraph">A class that is declared with abstract keyword, is known as abstract class in PHP. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. objects of an abstract class cannot be created.</p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span>
</span>{

}</code></span></pre>


<h3 class="wp-block-heading">Abstract Method:</h3>



<p class="wp-block-paragraph">A method that is declared as abstract and does not have implementation is known as abstract method</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>abstract function disp( ); //no body and abstract</p></blockquote>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span> 
</span>{
	<span class="hljs-keyword">abstract</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">disp</span> <span class="hljs-params">( )</span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">Class</span> <span class="hljs-title">Son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span>
</span>{
	<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">disp</span> <span class="hljs-params">( )</span>
	</span>{
		<span class="hljs-keyword">echo</span> “<span class="hljs-keyword">Abstract</span> defined”;
	}
}</code></span></pre>


<h3 class="wp-block-heading">Rules:</h3>



<ul class="wp-block-list"><li>We cannot use abstract classes to instantiate objects directly.</li><li>objects of an abstract class cannot be created.</li><li>The abstract methods of an abstract class must be defined in its subclass.</li><li>If there is any abstract method in a class, that class must be abstract.</li><li>A class can be abstract without having abstract method.</li><li>It is not necessary to declare all methods abstract in a abstract class.</li><li>We cannot declare abstract constructors or abstract static methods.</li><li>If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstract.</li></ul>



<h2 class="wp-block-heading">Difference between interface and abstract class:</h2>



<ul class="wp-block-list"><li>An abstract class can have only abstract methods or only non-abstract methods or both, but All methods of an interface are abstract by default.</li><li>An abstract class can declare properties/methods with access specifier, but interface can declare only constant properties and methods are by default abstract and public.</li><li>A class can inherit only one abstract class and multiple inheritance is not possible for abstract class but A class can implement more than one interface and can achieve multiple inheritance.</li><li>If a class contains even a single abstract method that class must be declared as abstract.</li><li>In an abstract class, you can defined as well as It’s body methods but in the interface you can only define your methods.</li></ul>
]]></content:encoded>
					
					<wfw:commentRss>https://www.bestdevops.com/how-to-create-a-class-in-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
