Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, we'll take a closer look at how the "scope" or "visibility" affect method access.
get_object_vars() - will only return the properties that can be accessed from the current scope. Therefore, it cannot access private or static properties of a child class.
Option 1: Adjust Visibility
From the video we adjusted the visibility to control which properties are used.
protected $description; // accessible
protected static $allowed_tags; // not accessible
Option 2: Extend Method
To extend the parent function, we would have to merge the arrays from the parent and the child. We still need to make sure that the visibility is correct, $allowed_tags would still need to be "static".
public function toArray()
{
return array_merge(
parent::toArray(),
get_object_vars($this)
);
}
Option 3: Manual Properties
We could use the getters to manually return the object properties as an array. This would give us precise control over what is returned, but this also increases the amount of manual work.
public function toArray() {
$array['id'] = $this->getId();
$array['title'] = $this->getTitle();
$array['website'] = $this->getWebsite();
$array['email'] = $this->getEmail();
$array['twitter'] = $this->getTwitter();
return $array;
}
We would then need to extend this class for the child.
public function toArray() {
parent::toArray();
$array['description'] = $this->getDescription();
return $array;
}
-
0:00
When we tried to upgrade a listing, we got an error.
-
0:03
No such column, allowed tags.
-
0:05
Let's go look at the update method.
-
0:08
All database interactions are part of the collection class so we need to look there.
-
0:12
[Blank Audio] update.
-
0:20
We pass the data to add a listing.
-
0:24
Then we call toArray on that listening to get the object properties as an array.
-
0:29
We then loop through the array to form the key value placeholders.
-
0:33
Let's take a look at the SQL that's generated.
-
0:41
And we'll just exit here.
-
0:46
We can see all the properties of the parent,
-
0:49
plus it added the allowed_tags property, but not the description.
-
0:54
Let's go back to the premium class properties.
-
0:59
Because the description is private, and the two-array method is on the parent,
-
1:05
the parent doesn't know anything about this private property.
-
1:08
We could override the method like we did with the set values method, but
-
1:12
let's see what else we can do.
-
1:14
We know that the protected visibility works
-
1:16
because the allowed tags are being added.
-
1:20
So we can set the description to protected.
-
1:26
Protected will let our parent class know about the property.
-
1:29
But still keep it from being accessed from outside the class.
-
1:33
We do not want the allowed tags in our listing so we could make that private.
-
1:38
But we may want to allow these to be overridden
-
1:41
I do want to make the allowed tags accessible without creating an object.
-
1:46
So I would need to make this property static.
-
1:49
So let's go ahead and make this protected static.
-
1:52
Now let's take a look in the browser again.
-
1:56
The SQL looks good, it has the description and not the allowed tags.
-
2:01
But now we're getting a notice.
-
2:03
Accessing static property listing premium allowed tags as non static.
-
2:09
Well that's why our two-array actually worked.
-
2:12
Because we can't access a static property
-
2:14
in the same way as we access a normal property.
-
2:17
Let's update our set description method.
-
2:26
To access a static property, we need to use the keyword self,
-
2:31
and then two colons, and then the property, starting with the dollar sign.
-
2:39
Let's try now.
-
2:42
Great, that fixed the error, and the SQL still looks good.
-
2:46
Let's go back and remove our debugging and see what happens.
-
2:57
Great, the listing has been updated,
-
2:59
but the listing page isn't showing the description.
-
3:02
Also, we never told the user which tags are allowed.
-
3:06
That's why I wanted the allowed tags to be static.
-
3:09
In the next video, we'll add some finishing touches.
You need to sign up for Treehouse in order to download course files.
Sign up