Home >Backend Development >PHP Tutorial >PHP object-oriented custom class_PHP tutorial

PHP object-oriented custom class_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:46786browse

  <span>所谓面向对象就是什么时候什么东西做什么,我们设计类的时候需要想的就是怎么做的内容,那么怎么样的一个类才算是符合OOP的思想呢,答案是:这个类写好之后,在使用的过程中,能准确的代表一个事物,在书写的时候代码要和思维描述一致,即这个东西做什么。<br />  那怎么开始设计一个合格的类呢,一开始就写class{}的都错了,正确的是什么都不写,而是假设这个类已经存在,这个对象已经存在,各种属性方法都已经有了,在这个完全的假设下想象下这个对象应该怎么用,例如我们制作一个缩略图的类,我们希望封装成一个类,方便下次使用,我们首先需要明确对象是什么它会做什么,要制作缩略图本质操作是缩小图片并输出,这里被操作的是图片,那么对象就是图片,由于网站上的图片不是唯一的我们得告诉这是那张图片,这就可以假设下这个类已经存在,一开始就得声明是那张图片,</span><span>例如</span><span>$simg = new simg("1.jpg");那么,一张图片应该有哪些属性?</span><span> 在制作缩略图的时候,我们最关心的应该是 宽,高,类型,而且这三项对一张图片而言是肯定的,这意味着这个对象一定有这些属性,</span><span>$simg->width,$simg->height,$simg->type,并且这些属性一开始就可以读取的到的,</span>
<span>1</span> <span>$simg</span> = <span>new</span> simg("1.jpg"<span>);
</span><span>2</span> <span>echo</span> <span>$simg</span>-><span>width;
</span><span>3</span> <span>echo</span> <span>$simg</span>-><span>height;
</span><span>4</span> <span>echo</span> <span>$simg</span>-><span>type;
</span><span>5</span> <span>//</span><span>这个对象,应该可以这样操作。</span>

<span>  根据oop的思想的原则,如果对象的属性被改变,对象应当也会发生相应的改变,这就意味着我们可以给它赋值,取得对象的宽度,高度,计算后(比如按比例缩小),重新赋值回去。</span><span>我们本质是要制作一张图片的缩略图,也就是生成一张新的图片,改变它之后,接下来要做的事情就应该是把这个改变过的图片存起来了,存起来是一个过程,所以它会是一个方法。 </span><span>例如 $simg->save(),考虑到要换一个地方存。至少要改一个名字吧,</span><span> 也就是说在使用的时候,这个对象应该是这样描述的,图片 保存到 &hellip;&hellip; 这意味着,这个方法,有一个参数,就是保存到哪。</span>
<span>$simg</span> = <span>new</span> simg("1.jpg");<span>//</span><span>实例化</span>
<span>$simg</span>->width = 200;<span>//</span><span>设置宽度</span>
<span>$simg</span>->height = 200;<span>//</span><span>设置高度</span>
<span>$simg</span>->save("2.jpg");<span>//</span><span>保存到2.jpg</span>

<span>在使用这个类的时候思维描述和写出来的代码应当完全一致,这里的思维描述出现了一个小问题,可能会产生不符合oop思维原则的误导,这里不符合面向对象的是:对象属性重新赋值 原图的大小为什么没有发生变化,变化的是另存出来的,也就是说这个对象其实是php内存中源对象的复制品,我们改变了复制品的大小并保存了下来,所以图片被真正改变之前图片的属性应当是只读的,改写是无效的,</span><span> 所以,如果以原图做为对象来描述的话。这样描述应该更准确:图片 改变大小后 另存为 。而原图的大小是没有发生改变的,改变大小是一个过程,这意味着这也是一个方法,</span>
<span>//</span><span>这个类,用起来应该是这样的。
//实例化一张图片</span>
<span>$simg</span> = <span>new</span> simg("1.jpg"<span>);
</span><span>//</span><span>读取图片宽高计算比例</span>
<span>$simg</span>-><span>width 
</span><span>$simg</span>-><span>height
</span><span>//</span><span>图片使用指定的宽高另存为&hellip;&hellip;</span>
<span>$simg</span>->size(200,200)->save("2.jpg");

<span>这是以原图做为对象的角度来描述的,尽管是不存在的一个类,但它的用法必须事先存在,并且符合oop的思想原则,即<span>这是个什么东西,它可以做些什么。</span>如果从另外的角度来思考呢,以即将被输出的图片作为对象呢,那么这个对象创建出来的时候它应该是空的,然后它必须以某张原图为基准,然后调整它的大小,再把它保存下来,</span>
<span>//</span><span>按这个思路描述。代码应该是这个样子</span>

<span>$simg</span> = <span>new</span> simg();  <span>//</span><span>一开始是空的</span>
<span>echo</span> <span>$simg</span>->width;   <span>//</span><span>肯定是 0</span>
<span>$simg</span>->load("1.jpg"); <span>//</span><span>以一张图为基准</span>
<span>echo</span> <span>$simg</span>->width;    <span>//</span><span>没改过,是原图大小

//改变大小</span>
<span>$simg</span>->width = 200<span>;
</span><span>$simg</span>->height = 200<span>; 
</span><span>$simg</span>->save("2.jpg");  <span>//</span><span>保存起来</span>

<span>此时看起来还不太明显<br />下面会更好:</span>
<span>$simg</span> = <span>new</span> simg("2.jpg");  <span>//</span><span>一开始是空的,指定一个文件名</span>
<span>$simg</span>->load("1.jpg"); <span>//</span><span>以一张图为基准
//改变大小</span>
<span>$simg</span>->width = 200<span>;
</span><span>$simg</span>->height = 200<span>; 
</span><span>$simg</span>->save();  <span>//</span><span>保存起来</span>

This will be more obvious, instantiating a thumbnail, but it does not exist yet, it will not exist on the hard drive until after saving.

 Here we first create this class according to the first method with the original image as the object. According to the above analysis, the following is as follows:

<?<span>PHP

</span><span>class</span><span> simg {
    </span><span>public</span> <span>$width</span> = 0<span>;
    </span><span>public</span> <span>$height</span> = 0<span>;
    
    </span><span>public</span> <span>function</span> __construct(<span>$img</span><span>) {
        
    }

    </span><span>public</span> <span>function</span> size(<span>$width</span>, <span>$height</span><span>) {
    
    }

    </span><span>public</span> <span>function</span> save(<span>$path</span><span>) {
    
    }
}</span>

<span>之后再根据对每个方法每个属性的要求,填上里面的代码,一开始就必须知道文件的高度,宽度,由于php处理不同类型的图片使用的函数不同,</span><span>我们在这里不得不知道文件类型是多少。以决定用哪个函数 设计类的时候,才是思考&ldquo;怎么做&rdquo;的时候,要在实例化之后,马上知道宽高。一定是在构造函数里完成的,只有构造函数会在类实例化的时候执行,这里我们可以使用 getimagesize 函数,取得文件的宽度,高度,类型 ,宽度和高度,我们可以在这里,赋值给属性。这样一来,实例化图片,就得到属性的问题就解决了,那</span><span>改变大小的过程呢?<br /></span>
<?<span>PHP

</span><span>class</span><span> simg {
    </span><span>public</span> <span>$width</span> = 0<span>;
    </span><span>public</span> <span>$height</span> = 0<span>;
    
    </span><span>public</span> <span>function</span> __construct(<span>$img</span><span>) {
        </span><span>$var</span> = <span>getimagesize</span>(<span>$img</span><span>);
        </span><span>$this</span>->width = <span>$var</span>[0<span>];
        </span><span>$this</span>->height = <span>$var</span>[1<span>];
    }

    </span><span>public</span> <span>function</span> size(<span>$width</span>, <span>$height</span><span>) {
    
    }

    </span><span>public</span> <span>function</span> save(<span>$path</span><span>) {
    
    }
}</span>

<span>  由于网络上常用的图片类型有gif jpg png 三种,其他的类型暂时不做考虑,调整大小的方法。在输出之前是什么也不做的。</span><span>可以说,我们的代码,只要知道要输出的图片是多大就可以了 但是,不同的方法,内部变量不通用。怎么办注册全局变量容易被外部变量干扰和污染,那我们就利用类的属性来保存。新加两个属性这里暂定为w和h,这两个属性,严格来说不是属性,只是我们利用属性来在方法之间传递变量而已,为了避免它们在类的外部被访问和修改我们在定义的时候使用关键字私有来进行访问限制,</span><span>private $w = 0;private  $h = 0;<br /></span>
  <span>public</span> <span>function</span> size(<span>$width</span>, <span>$height</span><span>) {
        </span><span>$this</span>->w = <span>$width</span><span>;
        </span><span>$this</span>->h = <span>$height</span><span>;
    }</span>

To change the size, just temporarily write down the width and height to be output. The following is saving. Before saving, you must first reduce the image size. Therefore, the thumbnail calculation process is mainly completed here. You need to load the original image to reduce it. Moreover, you must also know the file type. Because different types of images have different loading methods, file names and file types. We only know this in the constructor. At this time we add two more public properties, 


 <span>public</span> <span>$width</span> = 0<span>;
    </span><span>public</span> <span>$height</span> = 0<span>;
    
    </span><span>public</span> <span>$path</span> = ''<span>;
    </span><span>public</span> <span>$type</span> = 0<span>;
    
    </span><span>private</span>  <span>$w</span> = 0<span>;
    </span><span>private</span>  <span>$h</span> = 0<span>;
    
    </span><span>public</span> <span>function</span> __construct(<span>$img</span><span>) {
        </span><span>$var</span> = <span>getimagesize</span>(<span>$img</span><span>);
        </span><span>$this</span>->width  = <span>$var</span>[0<span>];
        </span><span>$this</span>->height = <span>$var</span>[1<span>];
        </span><span>$this</span>->path   = <span>$img</span><span>;
        </span><span>$this</span>->type   = <span>$var</span>[2<span>];   
    }</span>
After that, we can load the original image, change the size, and save it to the specified location in the save method. As for the save method, when writing different types of functions to call, you can choose to use switch ($var [2]) Make a judgment before creating a new thumbnail and save it.
<span>//</span><span>按这个类的编写方式。。使用方法应该是这样</span>

<span>$simg</span> = <span>new</span> simg("1.jpg"<span>);

</span><span>//</span><span>读取宽高并计算</span>
<span>$simg</span>-><span>width

</span><span>//</span><span>设置大小</span>
<span>$simg</span>->size(200, 200<span>);
</span><span>//</span><span>保存到</span>
<span>$simg</span>->save("2.jpg");
is a bit different from the description, because the description is: use (this) size, save as (here). This description is a bit convoluted. If you can write it like this, it will be fine. $simg->size(200, 200)- >save("2.jpg"); The use of the object must be such an object->Method (), which requires that the value in the previous quantity must be an object. The front of save is size, which requires the return value of size. It must be an object, but this method has nothing to return, and this object must be the current object, so there will be a save method. If there is no object, it doesn’t matter if we add one ourselves,
<span>public</span> <span>function</span> size(<span>$width</span>, <span>$height</span><span>) {
        </span><span>$this</span>->w = <span>$width</span><span>;
        </span><span>$this</span>->h = <span>$height</span><span>;
        </span><span>return</span> <span>$this</span><span>;
    }</span>

<span>返回当前的对象,这样就可以用</span><span>图片 使用(这个)大小,另存为(这里)$simg->size(200, 200)->save("2.jpg");这样一个符合oop思想的类封装完成了。</span>
<span><br /><br /></span>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781158.htmlTechArticleThe so-called object-oriented is when something does something. When we design a class, what we need to think about is how to do it. , so what kind of class is in line with OOP ideas,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn