$this is use for current class and view is method but what is load. Is this a property? Is this example correct? Answer Yes, load is a property. Think of it like this: This syntax is called chaining.
Tag: oop
Laravel 5.1 – BelongsTo relationship returns null
AppUser class User public function status() { return $this->belongsTo(‘AppUserStatus’, ‘user_status_id’, ‘id’); } AppUserStatus class UserStatus protected $fillable = [‘id’]; public …
Type hinting in PHP 7 – array of objects
Maybe I missed something but is there any option to define that function should have argument or return for example array of User objects? Consider the following code: <?php class User { …
Serialize/deserialize nested objects to JSON with type in PHP
I have classes that extends an abstract class. I need to create instances of these classes through a string – preferably JSON. Many of the objects are nested, and many properties are private. I need a way to: Create a JSON string of the complete object (with private properties and nested objects – with their private properties). Create a new
When should I use static methods?
I have a class that is containing 10 methods. I always need to use one of those methods. Now I want to know, which approach is better? OR Answer It is an interesting subject. I’m gonna give you a design oriented answer. In my opinion, you should never use a static class/function in a good OOP architecture. When you use
How to use class methods as callbacks
I have a class with methods that I want to use as callbacks. How can I pass them as arguments? Answer Check the callable manual to see all the different ways to pass a function as a callback. I copied that manual here and added some examples of each approach based on your scenario. Callable A PHP function is passed
call methods and objects using php
i have this code : in my code i don’t always want to use $test = new test …. I want to use the name directly instead of $test. For example: Answer You should use a factory method pattern or something like that. With a factory method you can create a class based on a string. In PHP it’s not
PHP 5.4: Getting Fully-qualified class name of an instance variable
I know there is a static class field on PHP 5.5, but I have to stick to PHP 5.4. Is it possible to get the fully qualified class name from a variable? Example: namespace MyAwesomeNamespace class …
php – check if class name stored in a string is implementing an interface
I understand that my question is somehow wrong, but I’m still trying to solve this problem. I have an interface Programmer: and a couple of namespaced classes: StudentsBjarneProgrammer (implements Programmer) StudentsCharlieActor (implements Actor) I have this class names stored in array $students = array(“BjarneProgrammer”, “CharlieActor”); I want to write a function, that will return an instance of class if it’s
when it is required that a class will be defined before instantiation?
In the PHP documentation it says: Classes should be defined before instantiation (and in some cases this is a requirement). can some one give me a example of a class that can not be instantiated unless it was previously defined? Answer The parsing rules are the same as for functions: if they’re defined in the “top level” of a file,