About __constructors and passing: what are they for? About parents: do they take the objects of siblings and vice versa?
Okay, so the book says: "when creating a new object, you can pass a list of arguments to the class being called. These are passed to a special method within the class, called the constructor, which initializes various properties.
I don't know what "passing" is, really, especially not in this context. I also read that if you use a constructor method, it takes the name of the class. So, if you have
class User
{
function __constructor($param1, $param2)
{
echo "User construct."
}
}
user();
What is supposed to happen? I can't tell because my pastebins say it's a "possible CRF attack", whatever that means....
Moving right along, we come to a later section in the book where code says:
$bothCatsObject = new tiger();
echo "Tigers have...";
echo "Fur: " . $bothCatObject->fur . "";
echo "Stripes: " . $bothCatObject->stripes;
class cat
{
public $fur; // Cats have fur.
function __construct()
{
$this->fur = "TRUE";
}
}
class tiger extends Cats
{
public $stripes; // Tigers have stripes.
function __construct()
{
parent::__construct(); // Call parent construct first.
$this->stripes = "True";
}
}
Is the point of this second block of code to show that when an object is called, its constructs activate and set the properties in active memory or something? Or, perhaps is it to say that when a properties is called, if the property its value is set within the constructor, it will have a value. If its value isn't set within a constructor (e.g., if it's set within a non-constructor method), an error will be returned?
The book says 'this code takes advantage of the benefits of constructor inheritance in the typical manner'. So, I'm hoping I'm not too far away. I'm wondering why constructors are useful. What are some applications where constructors are necessary, if I need to know that at this point in my study?
Finally, in this case, the "cats" class is never assigned an object. Because "tiger" extends the cats class, can the cats class be called via the tiger object? Here's an example of what I mean:
$bothCatsObject = new tiger();
$bothCatsObject->catTest(); //Does this return an error?
class cat
{
public $fur; // Cats have fur.
function __construct()
{
$this->fur = "TRUE";
}
function catTest()
{
echo "The object works for cats and tigers."
}
}
class tiger extends Cats
{
public $stripes; // Tigers have stripes.
function __construct()
{
parent::__construct(); // Call parent construct first.
$this->stripes = "True";
}
function tigerTest()
{
echo "The object works for tigers and cats."
}
}
Along these same lines, can I call the functions built into the "tiger" class from a new cat object because the tiger class is sibling to the cat class? In other words, would replacing lines 1 and 2 of the previous code block with this code work, or would it return an error:
开发者_Go百科
$bothCatsObject = new cat();
$bothCatsObject->tigerTest(); //Does this return an error?
First of all, run your script in your local machine.
ok :
class User
{
function __constructor($param1, $param2)
{
echo "My Name is {$param1} {$param2} ;)";
}
}
when you initialize the user class, it will automatically run the __construct function:
$user = new User("John","Smith");
// this will output "My Name is John Smith ;)"
Constructors run anytime you create a new instance of the class. an example is: Lets say you want to start a database connection and do some heavy work, you can write a a constructor that checks if there is already a db connection and if not create it and handles setting all the default configuations
$database = new Database();
// will automatically create a connection
// ex: mysql_connect("username","password","....
// mysql_select_db(...
// and so on
精彩评论