我如何构建" Class :: function() - > function($ parm) - > function($ parm);" PHP中的方法及其工作原理?

时间:2016-12-17 07:17:50

标签: php laravel

我想知道在一些PHP框架中使用类似的方法 Class::function()->function($parm)->function($parm); 以拉拉维尔为例 DB::table('users')->where('name', 'John')->value('email');

这些方法如何在内部工作?

我如何构建自己的这种方法?

请帮帮我。

1 个答案:

答案 0 :(得分:0)

Foo :: function() - > function Two()对公共函数的静态调用,返回一个新实例。

public class TableAandTableBActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks {
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_TABLEB);
   }

   @Override
   public Loader onCreateLoader(int id, Bundle args) {
       // Define a projection that specifies the columns from the table we care about.
       String[] projection = {
               TableAContract.TableAEntry._ID,
               TableAContract.TableAEntry.COLUMN_TableA_NAME,
               TableAContract.TableAEntry.COLUMN_TableA_QUANTITY,
               TableAContract.TableAEntry.COLUMN_TableA_CATEGORY,
               TableAContract.TableAEntry.COLUMN_TableA_PRICE};


       // Here is where I want to display the results of Select * from TableA, TABLEB where TableA._ID = TABLEB.productid
       return new CursorLoader(this,   // Parent activity context
               TableAContract.TableAEntry.CONTENT_URI,   // Provider content URI to query
               projection,             // Columns to include in the resulting Cursor
               null,                   // No selection clause
               null,                   // No selection arguments
               null);                  // Default sort order
   }

   @Override
   public void onLoadFinished(Loader loader, Object data) {

   }

   @Override
   public void onLoaderReset(Loader loader) {

   }
}



//

public class TableAProvider extends ContentProvider {

   private static final String LOG_TAG = TableAProvider.class.getSimpleName();
   static final int TableA = 100;
   private static final int TableA_ID = 101;
   static final int TABLEB = 200;
   private static final int TABLEB_ID = 201;
   static final int TableA_WITH_TABLEB = 300;
   private TableADbHelper mDbHelper;


   private static final SQLiteQueryBuilder sTableAWithTABLEB;


   private static final UriMatcher sUriMatcher = buildUriMatcher();

   static UriMatcher buildUriMatcher() {

       final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
       final String authority = TableAContract.CONTENT_AUTHORITY;

       matcher.addURI(authority, TableAContract.PATH_TableA, TableA);
       matcher.addURI(authority, TableAContract.PATH_TableA + "/#", TableA_ID);
       matcher.addURI(authority, TableAContract.PATH_TABLEB, TABLEB);
       matcher.addURI(authority, TableAContract.PATH_TABLEB + "/#", TABLEB_ID);
       matcher.addURI(authority, TableAContract.PATH_TableA + "/*", TableA_WITH_TABLEB);
       return matcher;
   }


   static{
       sTableAWithTABLEB = new SQLiteQueryBuilder();

       //This is an inner join which looks like
       //TableA INNER JOIN TABLEB ON TableA.id = TABLEB.TableA_id
       sTableAWithTABLEB.setTables(
               TableAContract.TableAEntry.TABLE_NAME + " INNER JOIN " +
                       TableAContract.TABLEBEntry.TABLE_NAME +
                       " ON " + TableAContract.TableAEntry.TABLE_NAME +
                       "." + TableAContract.TableAEntry._ID +
                       " = " + TableAContract.TABLEBEntry.TABLE_NAME +
                       "." + TableAContract.TABLEBEntry.COLUMN_TableA_ID);
   }



//

public final class TableAContract {

   private TableAContract() {
   }

   public static final String CONTENT_AUTHORITY = "com.test.androidtwotaabl.es";
   public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

   public static final String PATH_TableA = "TableA";
   public static final String PATH_TABLEB = "TABLEB";

   public static final class TableAEntry implements BaseColumns {

       public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_TableA);
       public static final String CONTENT_LIST_TYPE =
               ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TableA;
       public static final String CONTENT_ITEM_TYPE =
               ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TableA;

       public final static String TABLE_NAME = "TableA";
       public final static String _ID = BaseColumns._ID;
       public final static String COLUMN_TableA_NAME = "TableAName";
       public final static String COLUMN_TableA_QUANTITY = "TableAQuantity";
       public final static String COLUMN_TableA_PRICE = "TableAPrice";
       public final static String COLUMN_TableA_CATEGORY = "TableACategory";
       public final static String COLUMN_TableA_OLDPRICE = "oldPrice";

       public static Uri TableAWithTABLEB() {
           return CONTENT_URI.buildUpon().appendPath(PATH_TABLEB).build();
       }

   }

   public static final class TABLEBEntry implements BaseColumns {
       public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_TABLEB);
       public static final String CONTENT_LIST_TYPE =
               ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TABLEB;
       public static final String CONTENT_ITEM_TYPE =
               ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TABLEB;

       public final static String TABLE_NAME = "TABLEB";
       public final static String _ID = BaseColumns._ID;
       public final static String COLUMN_TableA_ID = "TableAId";
   }

}

请记住,这并不总是好的,因为它是对demeter https://en.m.wikipedia.org/wiki/Law_of_Demeter

的规律的推动