please help me understand this under code [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
public function save(PropelPDO $con = null)
{
if ($this->isNew() && !$this->getExpiresAt())
{
$now = $this->getCreatedAt() ? $this->getCreatedAt('U') : time();
$this->setExpiresAt($now + 86400 * sfConfig::get('app_active_days'));
}
return parent::save($con);
}
I don't understand return parent::save($con) please help me, thanks

It's calling the parent method save so whatever class this one extends from it's calling that method.
class Animal {
public function getName($name) {
return "My fav animal: " . $name;
}
}
class Dog extends Animal {
public function getName($name) {
return parent::getName($name);
}
}
$dog = new Dog();
echo $dog->getName('poodle');

This class extends a Propel Model class which also has a save() method. This save method overrides the parent's save() method. When this overridden save() is invoked, it first does some work related to this concrete class, then invokes the parent's save() which will persist the object's properties in database.

If you look at the class declaration, it will say something like
class ThisClass extends anotherClass.
The line you don't understand is returning the output of the save() method in anotherClass
parent
is the keyword that means "the class that this class is extending"
the :: (Scope Resolution Operator) allows you to call that method -- provided it is declared as static without instantiating the class --
Unless there is something else going on, you should be able to replace that line with
return $this->save($con);

The :: is the scope resolution operator. It allows you to access properties or methods from a class.
The parent keyword refers to the parent class of the current class.
With that in mind, your return statement is calling the save() method of your class' parent.

Related

PHP Classes:: Why declare as a new? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm very new to php classes and I was wonder why do I need to declare it to a variable and set it as NEW?
Here is an example :
class myFirstClass {
function Version(){
return 'Version 1';
}
function Info(){
return 'This class is doing nothing';
}
}
$hola = new myFirstClass;
echo $hola->Version();
Why this won't work WITHOUT declare it to a variable and set it to NEW ?
In other words... Without this line :
$hola = new myFirstClass;
I'm so used to functions so it looks weird to me...
This is a basic principle of Object Oriented Programming (OOP). Let's use a library system for example. If you want to get the name of a book, you cannot just say "give me the name of the book", you have to know what book (by id, author, whatever).
In functions, you can write one that looks like this:
function get_book($id){ // code }
In OOP it doesn't really work that way. You have a class book that keeps a name. But that name is only for that given book.
class Book {
var $name;
public __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
In order to call the getName() function we need to have a book. This is what new does.
$book = new Book("my title");
Now if we use the getName() function on $book we'll get the title.
$book->getName(); // returns "my title"
Hope that helps.
You are right! It is not necessary to use the new operator:
class myFirstClass {
static function Version(){// static keyword
return 'Version 1';
}
function Info(){
return 'This class is doing nothing';
}
}
echo myFirstClass::Version();// direct call
To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement).
If a string containing the name of a class is used with new, a new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.
The Basics
http://php.net/manual/en/language.oop5.basic.php
Classes and Objects
http://php.net/manual/en/language.oop5.php
This line:
$hola = new myFirstClass;
Is saying: create a new object called $hola, then put a new instance of myFirstClass into $hola. Now $hola is literally a object containing a new instance of myFirstClass.

What is the function __construct in PHP used for? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Can you give me an example that consist the function __construct and how that code would be if we convert it into procedural php? Maybe this is a kind of dummy question for you, but for me who never learning php oop before really got confused when i met this new function.
For example, in this code, function _construct used to instantiate a new class.
Class Car{
private $brand;
private $type;
public function __construct($merk, $type){
$this->brand= $brand;
$this->type = $type;
}
public function getCar() {
return "<strong>Brand:</strong> " . $this->merk . "<br /> <strong>Type:</strong> " . $this->type;
}
}
But I'm found new example which only contain this code :
Class Game{
public function __construct()
{
echo "Blah";
}
}
Why on the last example, function __construct just contain the word echo "Blah" not contain this code too --> $this->whatever word ?
The __construct method is usually the method called in a class when an instance of that class is created (called an object). The method is called the constructor because it constructs the object, but that doesn't mean you must use the method to do so.
The point of the method is to set the variables of the object to their inital values, which may be a primitive type such as an integer, or perhaps an instance of another class. If the variables are to be something other than a default value, the __construct method can accept arguments to set the values of the variables.
You don't have to set the variables using the constructor method, nor is it the only thing you can do. You could, for example, print a success message or call another method, either in that class or elsewhere. So your two examples are both valid. The first is how you would expect the constructor to be used, but the second is still vaild. Like I said, __construct is the method that is called on creation of the object, which doesn't mean you have to use it for the intended purpose.
In PHP, it is not required to have a constructor method in a class, but in many other object orientated programming languages, such as Java, if a constructor is not present then it will produce an error at compile time. This is because PHP is what is know as a weakly typed language, which has many pros and cons which you can research further.
Because __construct is one of the magic methods specifically used in a PHP class, it cannot be converted into procedural code. To call the method with arguments, when you assign a class in your code you should give your arguments in the creation statement, like this;
$object = new MyClass($arg1,$arg2);
You cannot call the constructor from outside the class, other than when creating a new object. The only exception to this is a child class (ie, a class that extends another class) can call the constructor of its parent using parent::__construct();.
The constructor is meant for to instantiate a new Class (not neccessarly though) like the example below:
<?php
Class Car{
private $brand;
private $type;
public function __construct($brand, $type){
$this->brand= $brand;
$this->type = $type;
}
public function getCar() {
return "<strong>Brand:</strong> " . $this->merk . "<br /> <strong>Type:</strong> " . $this->type;
}
}
$car= new Car('Audi', 'A3');
echo $car->getCar();
/* Will return:
Brand: Audi
Type: A3
*/

"Class contains 1 abstract method so must be declared abstract or implement the remaining methods (parent::hasErrors)" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This is my code:
interface ValidatorInterface
{
public function hasErrors ();
public function validate (stdClass $metadata);
public function getFeedback ();
}
abstract class ValidatorAbstract implements ValidatorInterface
{
protected $feedback;
public function __construct ()
{
$this->feedback = new FormFeedback();
}
public function getFeedback ()
{
return $this->feedback;
}
public function hasErrors ()
{
return $this->getFeedback()->hasErrors();
}
}
class RegistrationValidator extends ValidatorAbstract
{
public function validate (stdClass $metadata)
{
$this->getFeedback()->addError('Testing');
return !!$this->hasErrors();
}
}
This is the error:
Fatal error: Class RegistrationValidator
contains 1 abstract method and must therefore be declared abstract or
implement the remaining methods
(ValidatorInterface::hasErrors)
The abstract class ValidatorAbstract satisfies the requirements imposed by ValidatorInterface to have the hasErrors() and getFeedback() methods.
The class RegistrationValidator extends ValidatorAbstract and satisfies the last remaining requirement to declare a validate() method.
Why then am I getting an error saying that RegistrationValidator contains an abstract method, when it doesn't, and that it must implement hasErrors() when that method is already implemented by its parent class?
Make sure the code you posted is the same as the one you are running. In the example you posted, every abstract method is implemented somewhere in the hierarchy and the code runs fine.
Also make sure the file you are editing is the running copy (it could be cached).
In abstract class you need to have at least one abstract method.
abstract protected function someFunction();
A good practice is not to put any code into abstract classes.

Can you call a class from another class? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im currently learning oop from youtube etc and ive started my first project using it. The question i have that hasn't been answered along the way is..
say i have two classes
class a{
public function dosomething($var){
}
}
and
class b{
}
Can i access dosomething function from class b? if so could someone point me in the right direction.
Thanks
You have two options:
Pass instance of class a to class b and call method on that. (advisable)
Make method in class a static and call it like a::method(). (you should never do this)
To solve problem with the first way, your b class needs slight modification:
class b{
public function callMethodOfClassa(a $instanceOfa, $var) {
$instanceOfa->dosomething($var);
}
}
Or:
class b {
private $property;
public function callMethodOfClassa($var) {
$this->property->dosomething($var);
}
public function __construct(a $instanceOfa) {
$this->property = $instanceOfa;
}
}
In the second example you keep reference to passed instance in a field called $property here and is passed when instance of b is initialized:
$instanceOfa = new a();
$instanceOfb = new b($instanceOfa);
For better understanding of object oriented programming with php read the manual
And promised demo for the first example demo for the second sample (for better understanding made name changes).
Detailed explenation from comment:
class a{
public function dosomething(b $var){
$var->dosomething2();
}
public static function dosomething3(b $var){
$var->dosomething2();
}
}
and
class b{
public function dosomething2(a $var){
echo 'Hi, not doing anithing with this var!';
}
}
usage:
$variable1 = new a();
$variable2 = new b();
$variable1->dosomething($variable2);
a::dosomething3($variable2); //static call, no instance
$variable2->dosomething2($variable2);

pass to a function an object which is an instance of a subclass of the type accepted by the function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
is there any way who let the code below works?
public function my_function(My_Class $arg){ .... }
public class My_Sub_Class extends My_Class { ... }
//NOW
$my_object = new My_Sub_Class();
my_function($my_object);
I've Edited some write errors
public class My_Sub_Class extends My_Class() { }
should be
public class My_Sub_Class extends My_Class { }
Also, I'm not sure what $my_object = new My_Sub_Class(){ .... } is supposed to be. A My_Sub_Class is not a function!
Fixing these errors, adding a definition of My_Class to your testcase, and removing the public prefixes (because your example had no surrounding class definition), I end up with the following, which works just fine:
<?php
class My_Class {}
class My_Sub_Class extends My_Class {}
function my_function(My_Class $arg){ echo "my_function"; }
$my_object = new My_Sub_Class();
my_function($my_object);
?>
Take a look at the extends keyword in the PHP manual.
In fact, take a look at every feature you use in the manual, if you can't get something to "work".
You code works just fine (that is, passing a subclass to function that has a superclass typehint). Here's my code, verified to work on PHP 5.3.3:
<?php
class Foo {}
class Bar extends Foo {}
function quu(Foo $a) { return $a; }
$foo = new Foo();
$bar = new Bar();
var_dump(quu($foo));
var_dump(quu($bar));
Of course, you have a syntax error on your second and third line. But that has nothing to do with your question...
Since the child class extends parent class, it has to have all the properties and methods of the parent class. Because of that, you do not have to fear - your function will receive and work with objects of all the child classes as well.
You have an error in your third line - after initializing an object you do not need curly brackets.

Categories