An application _php technique for PHP inheritance

Source: Internet
Author: User
so that I
Think of inheritance to solve, previously published a session class, which is much simpler, through login to set the level of users, $session->get_status () of the return
A value of 0 indicates that the current user is not a blogger and therefore does not have permission to delete and edit the article. If the return value is 1, then the blogger himself is indicated. Good
, cut the crap. First up code
Copy Code code as follows:

Class Operationlimit
Operating limit. When no user login or was not ' this user
{
/* For limit the user Operat at post.
* @author: Xiaoai 8.12 2011
*/
Static $limitObject;
Public Function __construct () {}
When call the function but does not exist
public static function GetObject ()
{
if (!) ( Self:: $limitObject instanceof Self))
Self:: $limitObject = new Self;
Return self:: $limitObject;
}
protected function Setlimit () {}
Public Function Getreada ($postName)
{
Return ' <a herf=\ ' http://foodstory.me/post/'. $postname.
'. Php\ ' class=\ ' readmorelink\ ' >readmore</a> ';
}
}
Class Operationunlimit extends Operationlimit
When was this user
{
public static function GetObject ()
{
if (!) ( Self:: $limitObject instanceof Self))
Self:: $limitObject = new Self;
Return self:: $limitObject;
}
Public Function Getupdatea ($name)
{
Return ' <a href=\ ' http://foodstory.me/post/'. $name.
'. Php?do=update\ ' id=\ '. $name. ' \ ' >update</a> ';
}
Public Function getdelecta ($name)
{
Return ' <a href=\ ' javascript:delectpost ('. $name
.');\' Id=\ ' delectpost\ ' >delect</a> ';
}
}
Class Limitfactory
{
public static function Getlimitobject ($userStatus)
$userStatus = $session->get_status ();
{
Switch ($userStatus)
{
Case 0:
return Operationlimit::getobject ();
Case 1:
return Operationunlimit::getobject ();
Default
return Limit::getobject ();
}
}
}

Limitfactory is a factory class, also a static class. That is, there is no need to construct the object, its responsibility is to determine whether to return the Operationlimit class or an instance of the Operationunlimit class based on the incoming user permission value.
There are some common operations, such as reading more, the Operationunlimit class inherits this method, and then creates new methods, such as returning deletes and updating links.
Usage examples
Copy Code code as follows:

$LIMITOBJ = Limitfactory::getlimitobject ($session->get_status ());
echo $limitObj->getreada (' Hi ');
echo $limitObj->getdelecta (' hah ');

Here's a little bit irrelevant, just beginning when I didn't write the GetObject () static method in the Operationunlimit class, the discovery called
return Operationunlimit::getobject ();
The object that returns is the superclass, it feels strange, I am GetObject (), the method uses self to represent the current class, and does not indicate the object that must return the superclass. When in the child
This static method is overridden in a class to be OK. Later looked at Google, vaguely understand that the compiler at the beginning of the GetObject method and super class binding together, so
Calls in subclasses are still returning superclass objects.

Also, if you feel that there are so many escape characters in the string that are hard to distinguish, switch to
Echo <<<eeeeeee
<a href= ' foodstory.me/post/{$name}.php ' >read more</a>
eeeeeee;
It's a lot more refreshing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.